2

Is there any way in underscore.js to get the difference between two objects, tried with _.difference dosen't work. My object is like:

$scope.ABC = {};
$scope.ABC.fruits = {};
$scope.ABC.vegetables = {};
$scope.ABC.fruits.places = [];
$scope.ABC.fruits.type = [];
$scope.ABC.vegetables.places = [];
$scope.ABC.vegetables.type = [];

The id is common for ABC object. now I have another object XYZ same as ABC, but which can have places or types value different from ABC.

Any way I can find the difference between the two.

_.difference(ABC,XYZ) dosen't work.

Preena Khanuja
  • 205
  • 2
  • 6
  • 14

1 Answers1

0

This is what you are looking for: _.contains(list, value, [fromIndex])

Returns true if the value is present in the list. Uses indexOf internally, if list is an Array. Use fromIndex to start your search at a given index.

Now loop _.contains() for every attribute of $scope.ABC and check if it is present in $scope.XYZ. If not, push it e.g. into another array.

Offtopic: I suggest using lodash for various reasons

DonJuwe
  • 4,477
  • 3
  • 34
  • 59