Reputation: 155
I have an array of all items ids:
const allIds = ['a1gb', 'f4qa', 'i9w9']
I also have an object with it's properties having those ids as keys:
const byId = {
a1gb: {
whatever1
},
anyOtherIdThatIDontNeed: {
whatever444
},
f4qa: {
whatever2
},
i9w9: {
whatever3
}
}
What is the most common way to return an array that would look like
[ { whatever1 }, { whatever2 }, { whatever3 } ]
and skip the Ids I don't want in my final array?
This a log of the array with the ids:
This is a log of the object from which I need to return an array with the values of the keys from that array of ids skipping the ones I don't need:
P.S. Problem is that in that return array from the map function I get undefined when it encounters "anyOtherIdThatIDontNeed:".
P.P.S.[ ANSWER ] - The array of Ids had ids that do not match object's keys and that is why I was getting undefined.
Upvotes: 0
Views: 67
Reputation: 823
var result = allids.map(val => ({byId[val]}))
I would suggest this way, try the below code if the array also has unwanted ids.
var result = allids.map(val => ({byId[val]})).filter(val => val?true:false)
Upvotes: 1