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
4 Answers
Reset to default 1To 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
版权声明:本文标题:html - How to fetch the value in object array in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745206824a2647678.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论