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.
const other = { car: true, door: true, pen: true }; /* can be generated from array */ let result = things.filter(({ names }) => names.some(e => other[e] ?? false ));If your items aren't strings, use a Map instead, or just do something cheap likeincludesorindexOfonither_thingsdirectly, if you only have a couple dozen or hundred items at best.