Reputation: 561
I apologize in advance, couldn't come up with a better example for data.
So I have the array things
, which currently just consists of objects that contain an id
property and an array which just contains random things/words I could suddenly come up with, and other_things
, with with some random words from things
array.
let things=[
{
id: 1,
names: ['car','door','chair']
},
{
id: 2,
names: ['cat','dog','door']
},
{
id: 3,
names: ['phone','mouse','cat']
},
{
id: 4,
names: ['building','desk','pen']
},
{
id: 5,
names: ['road','date','number']
}
];
let other_things=['car','door','pen'];
What I'd like to achieve is to filter array things
, and only get those objects/elements, where the names
array contains at least one element from the other_things
array.
In this example, only things[2]
and things[4]
have no matches with any words from other_things
, so we don't need them.
I have already tried many things, like combining ES6 methods, like filter
, every
or map
, tried using nested for
loops, but nothing has worked out for me unfortunately.
Upvotes: 0
Views: 1285
Reputation: 55866
let things=[
{
id: 1,
names: ['car','door','chair']
},
{
id: 2,
names: ['cat','dog','door']
},
{
id: 3,
names: ['phone','mouse','cat']
},
{
id: 4,
names: ['building','desk','pen']
},
{
id: 5,
names: ['road','date','number']
}
];
let other_things=['car','door','pen'];
console.log(things.filter(t => t.names.filter(n => other_things.includes(n)).length > 0))
Upvotes: 3