To compare two arrays of objects,and exclude the elements who match values into new array in JavaScript, we use the filter
and some
methods.
For instance, we write
const result = result1.filter((o1) => !result2.some((o2) => o1.id === o2.id));
to call result1.filter
with a callback that checks if result2
don’t have items that have o2
in result1
‘s id
that’s equal to o2
in result2
‘s id
.
Then an array with the objects in result1
where id
property isn’t equal to objects in result2
‘s id
property are included.