Reputation: 1499
Let's say I have an array of userIds
const userIds = ['1234', '3212', '1122']
And then I have an array of objects
const arrayOfObjects = [
{
_source: {itemId: ['1234'] }
},
{
_source: {itemId: ['3212'] }
},
{
_source: {itemId: ['1111'] }
}
]
I want to filter my array of objects by matching ids to the userIds array
arrayOfObjects.filter(item => item._source.itemId === "what goes here?")
Upvotes: 0
Views: 47
Reputation: 5941
Array.prototype.includes
isn't supported by Internet Explorer. If you want a cross browser solution you could use Array.prototype.filter
and Array.prototype.indexOf
methods:
const userIds = ['1234', '3212', '1122'];
const arrayOfObjects = [{
_source: {
itemId: ['1234']
}
},
{
_source: {
itemId: ['3212']
}
},
{
_source: {
itemId: ['1111']
}
}
];
const filtered = arrayOfObjects.filter(o => userIds.indexOf(o['_source'].itemId[0]) > -1);
console.log(filtered)
Upvotes: 1
Reputation: 296
Try this
arrayOfObjects.filter(item => userIds.includes(item._source.itemId[0]))
Upvotes: 3