My use case: I have a two arrays one called "name" and the other called "customer". I need to extract the customers from the names list and get a new array called "lead".
const name = [{
id: 1,
name: "smith"
}, {
id: 2,
name: "john"
}]
const customer = [{
id: 1,
name: {
id: 1,
name: "smith"
}
}]
I am expecting to get
lead = [{
id: 2,
name: "john"
}]
the code I am using is
const name = [{
id: 1,
name: "smith"
}, {
id: 2,
name: "john"
}]
const customer = [{
id: 1 a,
name: {
id: 1,
name: "smith"
}
}]
const lead = name.filter(({
id: id1
}) => !customer.some(({
"name.id": id2
}) => id2 === id1));
console.log(lead);
This works if the data is flat, but when I use it with nested objects I get the full "name" list.
Working final code below
While Farrukh Normuradov answer does work, I used Pilchard's answer in the comments. I also fixed a typo.
Final code
const lead = name.filter(({ id: id1 }) => !customer.some(({ name: {id: id2} }) => id2 === id1));
leads arenames that are notcustomers?filterand!``some, I just think you have some syntax problems around "name.id".some()call is incorrect, it should be{ name: {id: id2} }( and the typo Mister JoJo mentioned)