When merging array of strings, I can use Set and ES6 to remove duplicates like so:
const a = ["hello", "hi", "yo"]
const b = ["alo", "hi"]
const remove_hi = [
...new Set([
...a,
...b,
]),
]
But how do I compare and remove objects? Say I have this:
const a = [
{id: "asd", name: "Hi"},
{id: "fgh", name: "Hello"},
{id: "123", name: "Hi"}
]
const b = [
{id: "jkl", name: "Yo"},
{id: "123", name: "Hi"}
]
// This will not work. It will have a duplicate of {id: "123", name: "Hi"}
const remove_hi = [
...new Set([
...a,
...b,
]),
]
How do I remove {id: "123", name: "Hi"} from a combined array with Set?
id, or by the combination ofidandname?name, do you want to keep the fist occurrence or the last occurrence (say the first object had a different id than the second one with the same name, which one should be kept)?