最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to compare two json objects using angularjs? - Stack Overflow

programmeradmin1浏览0评论

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 pare these two objects. I tried the following code:

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

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

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 pare these two objects. I tried the following code:

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

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

Share Improve this question asked Nov 3, 2015 at 6:09 J.JosephJ.Joseph 2342 silver badges11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

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

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

发布评论

评论列表(0)

  1. 暂无评论