[英]look for an object in an array of object return undefined
Hy, I have the following array of objects, I wish that the id returns to me its object. 嗨,我有以下对象数组,希望id返回给我它的对象。 How could I do? 我该怎么办?
prodotto=[
{
categoria:"primi",
prodotti:[
{
id:1,
nome:"pasta",
prezzo:12,
qta:0
},{
id:2,
nome:"gnocchi",
prezzo:12,
qta:0
}
]
},
{
categoria:"secondi",
prodotti:[ {
id:3,
nome:"salsiccia",
prezzo:12,
qta:0
},{
id:4,
nome:"frittura",
prezzo:12,
qta:0
}]
}
];
for example, after id 2 returns { id:2, nome:"gnocchi", prezzo:12, qta:0 } 例如,在id 2返回{id:2,nome:“ gnocchi”,prezzo:12,qta:0}之后
You can use .map()
and .find()
methods to find the desired object. 您可以使用.map()
和.find()
方法找到所需的对象。
let data = [{ categoria:"primi", prodotti:[ { id:1, nome:"pasta", prezzo:12, qta:0 }, {id:2, nome:"gnocchi", prezzo:12, qta:0 } ]}, { categoria:"secondi", prodotti:[ { id:3, nome:"salsiccia", prezzo:12, qta:0 }, {id:4, nome:"frittura", prezzo:12, qta:0 } ]}]; let locator = (objId) => [].concat(...data.map(({ prodotti }) => prodotti)) .find(( {id} ) => id === objId); console.log(locator(2));
You may also store the intermediate results in another array to easily iterate over them and find the resultant object. 您也可以将中间结果存储在另一个数组中,以轻松地对其进行迭代并找到结果对象。
let data = [{ categoria:"primi", prodotti:[ { id:1, nome:"pasta", prezzo:12, qta:0 }, {id:2, nome:"gnocchi", prezzo:12, qta:0 } ]}, { categoria:"secondi", prodotti:[ { id:3, nome:"salsiccia", prezzo:12, qta:0 }, {id:4, nome:"frittura", prezzo:12, qta:0 } ]}]; let _data = [].concat(...data.map(({ prodotti }) => prodotti)); let locator = (objId) => _data.find(( {id} ) => id === objId); console.log(locator(2));
You can use flatMap
and find
like this: 您可以使用flatMap
进行find
如下所示:
const prodotto = [{categoria:"primi",prodotti:[{id:1,nome:"pasta",prezzo:12,qta:0},{id:2,nome:"gnocchi",prezzo:12,qta:0}]},{categoria:"secondi",prodotti:[{id:3,nome:"salsiccia",prezzo:12,qta:0},{id:4,nome:"frittura",prezzo:12,qta:0}]}] const output = prodotto.flatMap(a => a.prodotti).find(p => p.id === 2) console.log(output)
If flatMap
isn't supported in your browser , use concat
and Spread syntax 如果浏览器不支持 flatMap
,请使用concat
和Spread语法
const prodotto = [{categoria:"primi",prodotti:[{id:1,nome:"pasta",prezzo:12,qta:0},{id:2,nome:"gnocchi",prezzo:12,qta:0}]},{categoria:"secondi",prodotti:[{id:3,nome:"salsiccia",prezzo:12,qta:0},{id:4,nome:"frittura",prezzo:12,qta:0}]}] const output = [].concat(...prodotto.map(a => a.prodotti)) .find(p => p.id === 2) console.log(output)
function getObjById(id) {
for (var i = 0; i < prodotto.length; i++) {
var obj = prodotto[i];
for (var j = 0; j < obj.prodotti.length; j++) {
var inn = obj.prodotti[j];
if (inn.id === id) return inn;
}
}
}
console.log(getObjById(4));
It's an old version of javascript, it's supported for ECMA5. 这是javascript的旧版本,ECMA5支持它。
One approach could be to use the Array#reduce
method combined with Array#find
. 一种方法是将Array#reduce
方法与Array#find
结合使用。 This allows you to iterate through each prodotto
array item and "reduce" this array down to the result that you're looking for (ie an item in the prodotti
sub-array with id
matching 2
). 这样,您就可以遍历每个prodotto
数组项,并将该数组“缩小”到所需的结果(即prodotti
子数组中id
匹配2
)。
During each reduce
iteration, the following logic allows you to either "pass through" a previously found result, or invoke a search for the sub-array item (via the Array#find
method): 在每个reduce
迭代期间,以下逻辑允许您“传递”先前找到的结果,或调用对子数组项的搜索(通过Array#find
方法):
// If currentResult defined, return that, otherwise look for a matching item
// in prodotti sub-array which will become the currentResult for subsequent
// iterations:
return currentResult || item.prodotti.find(subitem => {
return subitem.id === 2;
});
These two ideas combined into a working solution are shown as the following: 这两个想法组合成一个可行的解决方案,如下所示:
var prodotto = [{ categoria: "primi", prodotti: [{ id: 1, nome: "pasta", prezzo: 12, qta: 0 }, { id: 2, nome: "gnocchi", prezzo: 12, qta: 0 }] }, { categoria: "secondi", prodotti: [{ id: 3, nome: "salsiccia", prezzo: 12, qta: 0 }, { id: 4, nome: "frittura", prezzo: 12, qta: 0 }] } ]; // Use reduce to search all sub arrays of the prodotto array var result = prodotto.reduce((currentResult, item) => { // return the current result found, or if none yet return // the result (if any) that is found in the prodotti sub array return currentResult || item.prodotti.find(subitem => { return subitem.id === 2; }); }, undefined) console.log(result)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.