取出两个数组不同或者相同之处,当数组元素为对象时

取出两个数组不同或者相同之处,当数组元素为对象时
//取出两个数组不同之处
let userList = [{id:1},{id:2},{id:3},{id:4},{id:5},{id:6}];
let checkboxList = [{id:1},{id:2}];

//userList = userList.filter(item =>{return checkboxList.map(item => item.id).indexOf(item.id) === -1});

//ES6
userList = userList.filter(item =>{return !checkboxList.map(item => item.id).includes(item.id)});
console.log(userList);
//取出两个数组相同之处
let userList = [{id:1},{id:2},{id:3},{id:4},{id:5},{id:6}];
let checkboxList = [{id:1},{id:2}];

///userList = userList.filter(item =>{return checkboxList.map(item => item.id).indexOf(item.id) !== -1});

//ES6
userList = userList.filter(item =>{return checkboxList.map(item => item.id).includes(item.id)});
console.log(userList);

你可能感兴趣的:(javascript)