customerProducts: [
{
name: "foo",
id: 123
},
{
name: "test",
id: 44
}
]
otherProducts: [
{
name: "other",
id: 44
},
{
name: "test",
id: 21
}
]
我想遍历customerProducts
,它是一个对象数组。我想过滤customerProducts
的ID,该ID具有另一个对象数组otherProducts
的ID。因此,例如,在这种情况下,我希望返回的结果是:
{
name: "test",
id: 44
}
因为otherProducts
的{{1}}为44。
我当时想通过id
进行映射,然后返回一个ID数组,然后在其上运行otherProducts
,但这似乎是很长的路要走。
答案 0 :(得分:3)
答案 1 :(得分:0)
将customerProducts
,otherProducts
声明为JS数组变量,并使用JS Array filter find函数
let customerProducts = [
{
name: "foo",
id: 123
},
{
name: "test",
id: 44
}
]
let otherProducts = [
{
name: "other",
id: 44
},
{
name: "test",
id: 21
}
];
let filtered = customerProducts.filter( el => otherProducts.find( e => e.id == el.id) )
console.log(filtered);
答案 2 :(得分:0)