[英]filter array by integers by property of another array of objects
I have two arrays我有两个 arrays
var array1 = [1,2,3,4,5,6,2,1]
var array2 = [{id:1,name:"test"},{id:2,name:"test2"},{id:3,name:"test2"}]
I want to filter array1 by the id property of array2, so that, the result of array1 should be:我想通过 array2 的 id 属性过滤 array1,这样,array1 的结果应该是:
array1 = [4,5,6]
Suggestion: Please try before ask建议:请先试后问
const array1 = [1,2,3,4,5,6,2,1] const array2 = [{id:1,name:"test"},{id:2,name:"test2"},{id:3,name:"test2"}] const result = array1.filter(i =>.array2.find(x => x.id === i)) console.log(result)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.