Reputation: 75
I'm struggling hard to try and get a (what i though would be simple) filter to work.
I have an array of objects:
data:
{
0: {key: 'abc', name: ['bob', 'john', 'steve']},
1: {key: 'def', name: ['bob']}
}
I'm trying to an array to filter the object array:
filter: ['bob', 'john']
by using:
data.filter(v => v.name.includes(filter))
in the above, I would expect data[0] to be returned, and if I changed the filter to filter: ['bob']
then data[0,1] would be returned.
Unfortunately, nothing gets returned - and I can't quite seem to figure it out - it's probably simple, but any insight would be appreciated!
Upvotes: 1
Views: 40
Reputation: 386654
Assuming an array as data, you could filter by getting the name
array and check each filter item against this array.
var data = [{ key: 'abc', name: ['bob', 'john', 'steve'] }, { key: 'def', name: ['bob'] }],
filter = ['bob', 'john'],
result = data.filter(({ name }) => filter.every(f => name.includes(f)));
console.log(result);
Upvotes: 2