两个数组比较
let arr1 = [{id:'1',name:'json'},{id:'2',name:'book'} ]
let arr2 = [{id:'1',name:'json',age:'15'},{id:'2',name:'book',age:'16'},{id:'3',name:'ani',age:'17'}]
//ES6的方法
let add = arr2.filter(item => !arr1.some(ele=>ele.id===item.id));
cosnole.log(add);
//ES5的方法
for (var i=0; i
for (var j=0; j
if (arr2[i].id == arr1[j].id) {undefined
arr2.splice(i, 1);
}
}
}
注意上面删除的依据是数组中id字段,这个
js 两个数组对比,去除相同项得到新数组
var arr1 = [a, b, c,];
var arr2 = [a, b, c, d];
let list = arr2.filter(items => {undefined
if (!arr1.includes(items)) return items;
})
而上面的则是对字符串进行比较后去重
var arr1 = [a, b, c,];
var arr2 = [a, b, c, d];
let list = arr2.filter(items => {
if (!arr1.includes(items)) return items;
})
console.log(list);