[英]JS: Filter array of objects using second array of objects using ID
I have two object arrays:我有两个 object arrays:
let array1 = [{ id:'1232', name: 'foo'}, { id: '3442', name: 'moo'}, {id: '3144', name: 'coo'}]
let array2 = [{ id:'3442', value: 'some'}, { id:'5553', value: 'somevalue'}, {id: '3144', value: 'lala'}]
Using JS, how can I filter array1
by array2
so that the expected output could be:使用 JS,我如何通过
array2
过滤array1
以便预期的 output 可以是:
let filtered = [{ id: '3442', name: 'moo'}, { id: '3144', name: 'coo'}]
Thanks so much for your time!非常感谢您的宝贵时间!
combine filter and some to achieve this结合过滤器和一些来实现这一点
let array1 = [{ id:'1232', name: 'foo'}, { id: '3442', name: 'moo'}, {id: '3144', name: 'coo'}] let array2 = [{ id:'3442', value: 'some'}, { id:'5553', value: 'somevalue'}, {id: '3144', value: 'lala'}] output=array1.filter(x=>array2.some(y=>y.id==x.id)) console.log(output)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.