J.Joseph
J.Joseph

Reputation: 234

How to compare two json objects using angularjs?

I have two json objects

 $scope.car1={"Sedan":{"Audi":["A4","A3"]},"Hatchback":{"Maruthi":["Swift"]}}; 
  $scope.car2={"Hatchback":{"Maruthi":["Swift"]},"Sedan":{"Audi":["A3","A4"]}}; 

I want to compare these two objects. I tried the following code:

var a=angular.equals($scope.car1,$scope.car2);

Since angular.equalsdo a deep comparison it also care about the order of data. Is there any way to compare objects regardless of the order?

Upvotes: 4

Views: 9581

Answers (2)

Tomer W
Tomer W

Reputation: 3443

Implement a custom "recursive equalizer" that distinct if array is associative or not:
for associative => equalize keys, for arrays => sort array & equalize elements.

written the function myEqual() at this plunker

Upvotes: 1

Tarun Dugar
Tarun Dugar

Reputation: 8971

'Objects' do not have an order. angular.equals will compare values using their keys.

Upvotes: 2

Related Questions