我想要 2 个数组的新数组不等于数据

I want to new array of 2 array is not equal data

我想要新的数组 2 数组不等于数据 例子

let a = [{id:1, name:"a"},{id:2, name:"b"},{id:3, name:"c"}];

let b = [{id:1, name:"a"},{id:2, name:"b"},{id:3, name:"c"}, {id:4, name:"d"}];

结果

c = [{id:4, name:'d'}]

也许您正在寻找 set,请查看: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set

const a = [{id:1, name:"a"},{id:2, name:"b"},{id:3, name:"c"}]

const b = [{id:1, name:"a"},{id:2, name:"b"},{id:3, name:"c"}, {id:4, name:"d"}]

const bId = b.map(item => item.id)

const result = a.filter(item => bId.includes(item.id))

console.log(result)

您可以使用 filter()includes() 函数执行此操作。

let a = [{id:1, name:"a"},{id:2, name:"b"},{id:3, name:"c"}];
let b = [{id:1, name:"a"},{id:2, name:"b"},{id:3, name:"c"}, {id:4, name:"d"}];
// b diff a
let resultA = b.filter(elm => !a.map(elm => JSON.stringify(elm)).includes(JSON.stringify(elm)));

// a diff b
let resultB = a.filter(elm => !b.map(elm => JSON.stringify(elm)).includes(JSON.stringify(elm)));  

// show merge 
console.log([...resultA, ...resultB]);