Skip to content
Advertisement

Compare two arrays of objects (given the objects have same props, but not values)

Consider below two arrays of objects:

JavaScript

Comparison of these two objects must return false because there are values for prop id in arr2 that are missing in arr1 (namely id: 4).

At this point, the below attempt has been made:

JavaScript

NOTE: Suppose arr2 was:

JavaScript

The comparison must return true, since the id of every element in arr2 is found in arr1‘s elements.

Advertisement

Answer

You’re relaly close, but map is for mapping each element of the array to some new value, not for seeing if an element exists. You’d use some for that, and you’d want to reverse the arrays you’re calling every and some on:

JavaScript

That says: “Does every element of arr2 have at least one matching element in arr1 by id?”

Live Example:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement