我提前道歉,无法想出更好的数据示例。
所以我有一个数组 things
,它目前只包含包含 id
属性的对象和一个只包含我突然想出的随机事物/单词的数组,以及 {{1 }},带有来自 other_things
数组的一些随机单词。
things
我想要实现的是过滤数组 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'];
,并且只获取那些对象/元素,其中 things
数组至少包含 names
数组中的一个元素。
在此示例中,只有 other_things
和 things[2]
与 things[4]
中的任何单词都不匹配,因此我们不需要它们。
我已经尝试了很多东西,比如结合 ES6 方法,比如 other_things
、filter
或 every
,尝试使用嵌套的 map
循环,但没有任何效果不幸的是我。
答案 0 :(得分:3)
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))