let array = [
{name: "60", origin: "tcs"},
{name: "70", origin: "cfs"},
{name: "80", origin: "ehg"},
]
undefined
let def = [
{id: "60", testorigin: "tcs"},
{id: null, testorigin: "cfs"}, // this line should be removed
{id: "80", testorigin: "ehg"},
]
output
[
{name: "60", origin: "tcs"},
{name: "80", origin: "ehg"},
]
I just want to remove the second element of def because its id is null and its not matching with the name of array.
tried many way, but I failed, Thanks in advance.
>Solution :
You can do it like this:
let array = [
{name: "60", origin: "tcs"},
{name: "70", origin: "cfs"},
{name: "80", origin: "ehg"},
];
let def = [
{id: "60", testorigin: "tcs"},
{id: null, testorigin: "cfs"},
{id: "80", testorigin: "ehg"},
];
const result = array.filter(item => def.map(d => d.id).includes(item.name));
console.log('Result: ', result);