admin管理员组

文章数量:1415111

I would like to know how to get/fetch the object in nested array using javascript.

var value = "SGD"
var obj=[{
  country: singapore,
  ccy: ["EUR","SGD"]
  amount: "1000"
},{
  country: thailand,
  ccy: ["THB"]
  amount: "1000"
}]

function getData(){
 return obj.filter((e)=>{
    return ey == value; // fetch array object if it matches the value
  }
}

var result = getData();
console.log(result);

I would like to know how to get/fetch the object in nested array using javascript.

var value = "SGD"
var obj=[{
  country: singapore,
  ccy: ["EUR","SGD"]
  amount: "1000"
},{
  country: thailand,
  ccy: ["THB"]
  amount: "1000"
}]

function getData(){
 return obj.filter((e)=>{
    return ey == value; // fetch array object if it matches the value
  }
}

var result = getData();
console.log(result);

Share Improve this question asked May 15, 2019 at 8:19 miamia 2651 gold badge5 silver badges17 bronze badges 5
  • 2 ey.includes(value) should work – georg Commented May 15, 2019 at 8:21
  • you'll want a bination of map/filter ... or maybe find or something – Jaromanda X Commented May 15, 2019 at 8:21
  • @georg - no, includes returns a boolean, not an object – Jaromanda X Commented May 15, 2019 at 8:21
  • @JaromandaX: I have no idea what the OP is asking, and neither do you. Until they post their code and example output all we can do is to make more or less accurate guesses. – georg Commented May 15, 2019 at 8:24
  • 1 I know exactly what they are asking @georg - the clue is in the question ... fetch array object if it matches the value – Jaromanda X Commented May 15, 2019 at 8:25
Add a ment  | 

4 Answers 4

Reset to default 1

To get the array of objects that includes the selected currency in the variable value you can use Array.prototype.filter() bined with Array.prototype.includes():

const value = 'SGD';
const obj = [{country: 'singapore',ccy: ['EUR', 'SGD'],amount: '1000'}, {country: 'thailand',ccy: ['THB'],amount: '1000'}];
const getData = (arr, value) => arr.filter(o => oy.includes(value));

const result = getData(obj, value);
console.log(result);

Note that instead of a function getData using variables out of function scope it is better pass the parameters you need in the function getData(obj, value)

It's hard to tell from your question, but if you want the first matching entry in the array, you're looking for the find method, using includes on ccy in the search:

function getData(){
    return obj.find(e => ey.includes(value));
}

Live Example:

var value = "SGD";
var obj= [{
  country: "singapore",
  ccy: ["EUR","SGD"],
  amount: "1000"
},{
  country: "thailand",
  ccy: ["THB"],
  amount: "1000"
}];

function getData() {
  return obj.find(e => ey.includes(value));
}

var result = getData();
console.log(result);

ey is an array. Comparing it with any other variable will never return true unless both have same reference. To check if the element is present in array using Array.prototype.includes()

var value = "SGD";
var obj=[{
  country: 'singapore',
  ccy: ["EUR","SGD"],
  amount: "1000"
},{
  country: 'thailand',
  ccy: ["THB"],
  amount: "1000"
}]

function getData(){
 return obj.filter((e)=>{
    return ey.includes(value)
  })
}

var result = getData();
console.log(result);

var obj=[{
  country: 'singapore',
  ccy: ["EUR","SGD"],
  amount: "1000"
},{
  country: 'thailand',
  ccy: ["THB"],
  amount: "1000"
}]

function getData(val) {
   var result = obj.find(function(o) {
   return oy.indexOf(val) > -1;
   });
return result;
}

console.log(getData('SGD'));

本文标签: htmlHow to fetch the value in object array in javascriptStack Overflow