2

I have two json objects and I want to compare them with each other. Both with the same values but in different order. Now I found this useful function in angular called angular.equals() that tells me whether the objects are the same or not, however I'm trying to figure out a way that this function ignores the order the values are in. For example

Edit made to the code

var obj = {{name: "Product 1"}, {name:"Product 2"}, {name:"Product 3"}}
var obj2= {{name: "Product 2"}, {name:"Product 1"}, {name:"Product 3"}}

As you can see the values are the same, except they are in a different order. Is there any way for angular to ignore the order?

BigBawss
  • 85
  • 12
  • 2
    That is not valid JavaScript object notation. Do you mean arrays? – str Oct 09 '16 at 21:01
  • Does the array data need to stay in the same order? – Tony Hensler Oct 09 '16 at 21:01
  • @str I editted the code slightly to how it actually looks, note that angular.equals returns true on that if they were in the same order but in this case it returns false because the order isn't the same, thus the object isn't the same. – BigBawss Oct 09 '16 at 21:13
  • @TonyHensler The order of array data can be different sometimes, that's why I want to compare the values and not the order. angular.equals checks if the order of the values are equal as well and I don't want that. – BigBawss Oct 09 '16 at 21:16

2 Answers2

0

You could use _.isEqual() from Lodash

Example:

var arr1 = ["Product 1", "Product 2", "Product 3"];
var arr2 = ["Product 2", "Product 1", "Product 3"];

_.isEqual(arr1, arr2);

For pure javascript, there is another question related: How to compare arrays in JavaScript?

The other alternative would be comparing strings:

var equal = JSON.stringify(arr1.sort()) == JSON.stringify(arr2.sort())
Community
  • 1
  • 1
Edmar Miyake
  • 12,047
  • 3
  • 37
  • 38
  • Is there a way to do it with angular? Because angular automaticly adds a property of it's own called $$hashKey with a unique value to every array/object I make. if I use Lodash or the pure Javascript method, the will comparison will never be true because the property $$hashKey is always unique. – BigBawss Oct 09 '16 at 21:34
  • angular.equals() - see my answer – Andriy Oct 09 '16 at 21:53
0
angular.equals(["Product 1", "Product 2", "Product 3"].sort(), ["Product 2", "Product 1", "Product 3"].sort());

please note, that arrays are sorted, otherwise it will return false (as well as with lodash). Hash keys are ignored by this function.

if array items are complex objects,we can sort array by object property passing to sort function our comparison function:

sort(function (a, b) {
  // our comparison code (a.id against b.id for example)
})

angular.equals([{name: "Product 1"}, {name:"Product 2"}, {name:"Product 3"}].sort(function(a,b){return a.name > b.name}), [{name: "Product 2"}, {name:"Product 1"}, {name:"Product 3"}].sort(function(a,b){return a.name > b.name}))

will return true

Andriy
  • 14,781
  • 4
  • 46
  • 50