Why spread is not removing the duplication from the Items?
I am using spread to merge two variables resultA
and resultB
. There are two duplicated Id (222) in DataA and DataB which should be removed when using spread. Why it did not happen and how to solve this?
const DataA = [
{ Id: 111, Name: 'A' },
{ Id: 222, Name: 'B' },
{ Id: 333, Name: 'C' }
]
const DataB = [
{ Id: 999, Name: 'A' },
{ Id: 222, Name: 'B' },
{ Id: 444, Name: 'C' }
]
let resultA = (DataA || []).map(item => item.Id);
let resultB = (DataB || []).map(item => item.Id);
const items = [
...resultA,
...resultB,
]
const mapping = {
Total: items.length,
Items: items.map(item => { return { Id: item }})
}
console.log(mapping);
Expected Result:
{ Total: 5,
Items:
[ { Id: 111 },
{ Id: 222 },
{ Id: 333 },
{ Id: 999 },
{ Id: 444 } ]
}