根据id数组获取对应的另一数组的数据
let list =[{id:1,name:'天'},{id:2,name:'气'},{id:3,name:'真'},{id:4,name:'好'}]
/*举例一*/
let ids1 = [1, 2]
let check1 = list .filter(item => ids1.some(ele => ele === item.id)); //取两数组的交集
/*举例二*/
let ids2 = [{id:1,name:'天'},{id:2,name:'气'}]
let check2 = list .filter(item => ids2.some(ele => ele.id === item.id)); //取两数组的交集
//或者
let check3 = list.filter(item => ids2.indexOf(item.name) > -1)
/*举例三*/
// 求两数组的差集
let check4 = list.filter(item => !ids2.some(ele => ele.id === item.id))
网友评论