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.
CodePudding user response:
This is solution. you just need to check condition of both objects with same indexes. this answer will help you.
for(let i=0;i<array.length;i ){
if(array[i].name == def[i].id){
console.log(array[i])
}
}
Thanks!
CodePudding user response:
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);