matching object of array on a condition

Viewed 23

I have an array like this:

[{id: 1, name: 'abc1'}, {id: 2, name: 'abc2'}, {id: 3, name: 'abc3'}, {id: 4, name: 'abc4'}]

How can I get the object of array that matches a condition, with entire rows array?

My code:

var forms = [{id: 1, name: 'abc1'}, {id: 2, name: 'abc2'}, {id: 3, name: 'abc3'}, {id: 4, name: 'abc4'}];
var rows = [1,3];

for (var index in rows)  //my logic is not working
{
const abc = () =>{
   let result = forms.filter(v => v.id !== rows[index]);
   return result;
   }
abc();
}

How can I implement it using JavaScript?

So here is the result I want to get:

[ {id: 2, name: 'abc2'}, {id: 4, name: 'abc4'}];
1 Answers

You just need a single .filter - check that the object's id isn't included in the rows array.

var forms = [{id: 1, name: 'abc1'}, {id: 2, name: 'abc2'}, {id: 3, name: 'abc3'}, {id: 4, name: 'abc4'}];
var rows = [1,3];

const result = forms.filter(obj => !rows.includes(obj.id));
console.log(result);

Related