0
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.

NeNaD
  • 18,172
  • 8
  • 47
  • 89

2 Answers2

0

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);
NeNaD
  • 18,172
  • 8
  • 47
  • 89
0

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!

YOGENDRA SINGH
  • 118
  • 1
  • 1
  • 18