题目描述
var arr = [1,2]; var allArr = [{id:1,name:"s"},{id:2,name:"ds"},{id:3,name:"gg"}] var finalArr = []; // 最终结果 finalArr = [{id:3,name:"gg"}];
解决方法
var arr = [1,2]; var allArr = [{id:1,name:"s"},{id:2,name:"ds"},{id:3,name:"gg"}] // 最终结果 // finalArr = [{id:3,name:"gg"}]; var finalArr = allArr.filter((item) => !arr.includes(item.id));
includes() 方法用来判断一个数组是否包含一个指定的值,如果是返回 true,否则false。
[1, 2, 3].includes(2); // true [1, 2, 3].includes(4); // false [1, 2, 3].includes(3, 3); // false [1, 2, 3].includes(3, -1); // true [1, 2, NaN].includes(NaN); // true