How To Get A List Of Objects By Their Id Using An Array Of Numbers
NOTE: I understand that the title is phrased ambiguously and the explanation below is simplistic, apologies I'm a little new to JS I have this array of objects (this.listOfAnimals)
Solution 1:
Use map()
to iterate over arrayOfNumbers
, and use the values as indexes into listOfAnimals
let selectedAnimals = this.arrayOfNumbers.map(n =>this.listOfAnimals[n]);
Solution 2:
try this
this.arrayOfNumbers.map(id => { console.log(this.listOfAnimals[id]); });
Post a Comment for "How To Get A List Of Objects By Their Id Using An Array Of Numbers"