How to compare two jsons ignoring order of elements in array properties?
Use JSONAssert
They have a loose assert.
Loose:
JSONAssert.assertEquals(exp, act, false);
Strict:
JSONAssert.assertEquals(exp, act, true);
I don't know if such thing exist, but you can implement it yourself.
var group1 = { id: 123, users: [ {id: 234, name: "John"}, {id: 345, name: "Mike"} ]};var group2 = { id: 123, users: [ {id: 345, name: "Mike"}, {id: 234, name: "John"} ]};function equal(a, b) { if (typeof a !== typeof b) return false; if (a.constructor !== b.constructor) return false; if (a instanceof Array) { return arrayEqual(a, b); } if(typeof a === "object") { return objectEqual(a, b); } return a === b;}function objectEqual(a, b) { for (var x in a) { if (a.hasOwnProperty(x)) { if (!b.hasOwnProperty(x)) { return false; } if (!equal(a[x], b[x])) { return false; } } } for (var x in b) { if (b.hasOwnProperty(x) && !a.hasOwnProperty(x)) { return false; } } return true;}function arrayEqual(a, b) { if (a.length !== b.length) { return false; } var i = a.length; while (i--) { var j = b.length; var found = false; while (!found && j--) { if (equal(a[i], b[j])) found = true; } if (!found) { return false; } } return true;}alert(equal(group1, group2))
You could slice the arrays, sort them by Id then stringify them to JSON and compare the strings. For a lot of members it should work pretty fast. If you duplicate Ids, it will fail because sort will not change the order.