Looking to remove an object from an array if it's not present in another array. After doing some research, I came across a similar question on this link, but with a different array source.
Below is the example from the link:
var check = [1, 2, 3];
var allowed = [1];
var filtered = check.filter(function(item) {
return allowed.indexOf(item) > -1;
});
console.log(filtered);
The example above removes numbers from the array check
if they are not present in the array allowed
. In my case, the source array contains objects as shown below:
var check = [
{
name: 'Peter',
location: 'florida'
},
{
name: 'Robert',
location: 'California'
}
];
var allowed = [
{
name: 'Robert',
location: 'Boston'
}
];
var filtered = check.filter(function(item) {
return allowed.indexOf(item.name) > -1;
});
console.log(filtered);
I ran the code and ended up with an empty array as the result.
Expected result:
[
{
name: 'Robert',
location: 'California'
}
]
If anyone could help me achieve the expected result, that would be greatly appreciated.