I have two array of objects, filter and users. i want to loop through each user and check if his name is matching the filter.
var filter = [
{name: 'John'},
{name:'Tom'}
]
var users = [{
name: 'John',
email: 'johnson@mail.com',
age: 25,
address: 'USA'
},
{
name: 'Tom',
email: 'tom@mail.com',
age: 35,
address: 'England'
},
{
name: 'Mark',
email: 'mark@mail.com',
age: 28,
address: 'England'
}
];
I have tried this, but no luck.
users= users.filter(function(item) {
filter.forEach(function (key) {
if (item.name=== undefined || item.name == filter[key.name])
return false;
})
return true;
});
console.log(users)
I'm looking for a result like.
[
{
name: 'John',
email: 'johnson@mail.com',
age: 25,
address: 'USA'
},
{
name: 'Tom',
email: 'tom@mail.com',
age: 35,
address: 'England'
}
]