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.

12
  • 4
    have you tried anything yourself? if so show us the code Commented Jun 25, 2015 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... Commented Jun 25, 2015 at 13:21
  • 3
    Duplicate: stackoverflow.com/questions/1773069/… Commented Jun 25, 2015 at 13:22
  • Do the objects need to be in the same order? Commented Jun 25, 2015 at 13:23
  • You should define an equals method for your objects. Commented Jun 25, 2015 at 13:23

2 Answers 2

2

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 ;)

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

4 Comments

Yes.. I read somewhere that it's not a good practice to convert them to strings. Don't know the reason though..
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.
Yup.. Worked like a charm... :)
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.
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

Comments

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.