Lodash / javascript : Compare two collections and return the differences

Nacim Idjakirene picture Nacim Idjakirene · Nov 17, 2016 · Viewed 65.8k times · Source

I have two array of object :

the element of my table are not primitive value, but complexe objects.

array1 = [obj1,obj2,obj3,obj4]
array2 = [obj5,obj5,obj6,obj7]

I would like to compare two arrays and see of the elements of array 2 are already present in array1 then create a new array of the difference.

Any suggestion ?

Answer

stasovlas picture stasovlas · Nov 17, 2016
var presents = _.intersectionWith(array1, array2, _.isEqual);
var dif = _.differenceWith(array1, array2, _.isEqual);