简体   繁体   English

JS:使用 ID 的第二个对象数组过滤对象数组

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM