I have an array of objects that I’m trying to filter by a list of values:
var idlist = [
{
id: '1',
},
{
id: '3',
},
{
id: '2',
},
{
id: '6',
},
]
var values = ['1', '6', '3']
one way i’ve found to do this is to convert my list into an object and then filter using
value.filter(k =>
obj.find(v => v.id === k.id)
)
However, I feel like that’s an extra step I don’t need. Is there a simpler solution?
The output i’m looking for is
new idlist
[
{
id: '1',
},
{
id: '3',
},
{
id: '6',
},
]
Thanks!
>Solution :
Simply use
idlist.filter(f => values.includes(f.id))
var idlist = [{
id: '1',
},
{
id: '3',
},
{
id: '2',
},
{
id: '6',
},
]
var values = ['1', '6', '3']
console.log(idlist.filter(f => values.includes(f.id)))