0

I have two array with 5 objects each. Before I proceed, I want to check whether these two arrays are equal or not. I tried looking for the answers but unfortunately couldnt find anything for nested arrays with multiple objects. Is there a way to achieve this?

eg. Array1 ==> 5 nested objectsenter image description here Array2 ==> 5 nested objects

Now check whether Array1 == Array2 and return a boolean value.

Gaurav Sachdeva
  • 75
  • 2
  • 3
  • 9
  • 4
    have you tried anything yourself? if so show us the code – depperm Jun 25 '15 at 13:21
  • You need to compare the objects individually compare `index 0` to `index 0` compare all the `attributes` of each object for equality... – brso05 Jun 25 '15 at 13:21
  • 3
    Duplicate: http://stackoverflow.com/questions/1773069/using-jquery-to-compare-two-arrays-of-javascript-objects – Tuhin Jun 25 '15 at 13:22
  • Do the objects need to be in the same order? – Timmetje Jun 25 '15 at 13:23
  • You should define an equals method for your `objects`. – brso05 Jun 25 '15 at 13:23
  • possible duplicate of [How to check if two arrays are equal with JavaScript?](http://stackoverflow.com/questions/3115982/how-to-check-if-two-arrays-are-equal-with-javascript) – Patsy Issa Jun 25 '15 at 13:24
  • @depperm: YES, I did try but couldn't figure out a way.Thats why I came here for help. AreArraysIdentical: function (DefaultSortOrder, NewSortOrder) { var i = DefaultSortOrder.length; if (i != NewSortOrder.length) return false; while (i--) { if (DefaultSortOrder[i] !== NewSortOrder[i]) return false; } return true; }, I am not able to browse through nested objects here. – Gaurav Sachdeva Jun 25 '15 at 13:25
  • @TimDev : Yes they need to be in the same order for equality condition to hold. – Gaurav Sachdeva Jun 25 '15 at 13:26
  • do you actually have nested obj or do you just have two arrays of 5 obj? – depperm Jun 25 '15 at 13:26
  • @depperm: two arrays with 5 objects each. – Gaurav Sachdeva Jun 25 '15 at 13:28
  • Array1 : 0: Object 1: Object 2: Object 3: Object 4: Object 5: Object Array2 : 0: Object 1: Object 2: Object 3: Object 4: Object 5: Object – Gaurav Sachdeva Jun 25 '15 at 13:30
  • have you taken a look at http://stackoverflow.com/questions/29666341/is-there-a-way-to-add-subtract-all-fields-inside-a-json-with-another-object-with/29666705#29666705 ? it would be fairly easy to modify for your needs – Tschallacka Jun 25 '15 at 13:39

2 Answers2

3

You can do it without jQuery - change them to a string by using globally available JSON.stringify method, then the comparison will be easy:

JSON.stringify(arr1) === JSON.stringify(arr2);

This is kind of a hack. But it does work well. And in the era when Angular framework is checking it's injections by running toString() on its functions and then regexping the attributes (oh yes it does), I think this is just an effective solution ;)

Tomek Sułkowski
  • 7,081
  • 2
  • 17
  • 15
  • Yes.. I read somewhere that it's not a good practice to convert them to strings. Don't know the reason though.. – Gaurav Sachdeva Jun 25 '15 at 13:36
  • There's no such thing as a bad practice by default. In this case it's an effective solution, in other cases it **might** be bad practice. My point being, it's subjective and entirely depending on the situation. – Timmetje Jun 25 '15 at 13:41
  • Yup.. Worked like a charm... :) – Gaurav Sachdeva Jun 25 '15 at 13:41
  • It depends on your objects structure. The only downsides I know is that it wont be able to change date objects back from string values, and you'd lost eventual custom methods of your objects. – Tomek Sułkowski Jun 25 '15 at 13:41
0
var arr1 = [{val: 1}, {val: 2}, {val: 3}, {val: 4}, {val: 5}]
var arr2 = [{val: 1}, {val: 2}, {val: 3}, {val: 4}, {val: 5}]
var arr3 = [{val: 1}, {val: 2}, {val: 3}, {val: 9}, {val: 5}]

function isEqual(arr1, arr2) {

    // If the array lengths are different, return false
    if (arr1.length !== arr2.length) return false;

    // Grab an array of true/false values determined
    // by the result of the `some` callback
    // Return false if `false` is found in the array, otherwise true
    return arr1.map(function (one) {
        return arr2.some(function (two) {
            return two.val === one.val;
        });
    }).indexOf(false) > -1 ? false : true;
}


isEqual(arr1, arr2); // true
isEqual(arr1, arr3); // false

DEMO

Andy
  • 61,948
  • 13
  • 68
  • 95