Filter Array Not in Another Array
You can simply run through obj1
using filter
and use indexOf
on obj2
to see if it exists. indexOf
returns -1
if the value isn't in the array, and filter
includes the item when the callback returns true
.
var arr = obj1.filter(function(item){
return obj2.indexOf(item.id) === -1;
});
With newer ES syntax and APIs, it becomes simpler:
const arr = obj1.filter(i => !obj2.includes(i.id))