let arr = [4, 5, 2];
, второй с объектами:let people = [
{
id: 1,
firstName: 'Alexey',
},
{
id: 2,
firstName: 'Slava'
},
{
id: 3,
firstName: 'Vlad'
},
{
id: 4,
firstName: 'Andrey',
},
{
id: 5,
firstName: 'Mikhail'
},
{
id: 6,
firstName: 'Stepan'
}
]
[
{
id: 2,
firstName: 'Slava'
},
{
id: 4,
firstName: 'Andrey',
},
{
id: 5,
firstName: 'Mikhail'
}
]
arr.map(id => people.find(n => n.id === id))
// или
arr.map(function(n) {
return this[n];
}, people.reduce((acc, n) => (acc[n.id] = n, acc), {}))
people.filter(n => arr.includes(n.id))
// или
people.filter(((ids, n) => ids.has(n.id)).bind(null, new Set(arr)))