let arrayList1 = [ { id: 1, content: 'aaa' }, { id: 2, content: 'bbb' }, { id: 3, content: 'ccc' }, ]; let arrayList2 = [ { id: 1, content: 'aaa' }, { id: 4, content: 'bbb' }, { id: 5, content: 'ccc' } ];
let newArray = arrayList1.filter((item) => !arrayList2.some((item2) => item2.id === item.id)); let newArray2 = arrayList2.filter((item) => !arrayList1.some((item2) => item2.id === item.id)); console.log('newArray', newArray); console.log('newArray2', newArray2);
let newArray3 = []; let newArray4 = []; arrayList1.forEach(item => { if (arrayList2.every(res => res.id !== item.id)) { newArray3.push(item) } }) arrayList2.forEach(item => { if (arrayList1.every(res => res.id !== item.id)) { newArray4.push(item) } })
console.log(newArray3, "newArray3") console.log(newArray4, "newArray4")
filter 用法 JavaScript Array filter() 方法 | 菜鸟教程