4

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?

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

The 'Sedan' key's value 'Audi' has different arrays (the order is different in these arrays).
Well, technically, Tarun, you are right, but still this is his question: put, given two arrays, how can i compare them order-less (note: recursively)
ok, I was under the assumption that he was comparing objects not arrays. My bad.
1

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

3 Comments

I don't need to use underscore.js in this case. I need pure javascript or angularjs.
well, you can easily implement another one, i'll post some code later when i find the time.
there are no associative arrays in javascript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.