简体   繁体   中英

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. 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 }

You can use .map() and .find() methods to find the desired 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 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:

 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

 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.

One approach could be to use the Array#reduce method combined with 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 ).

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):

// 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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM