简体   繁体   中英

filter array by integers by property of another array of objects

I have two 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:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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