2

I'm trying to compare two arrays both 2d, I need it only match when they're completely identical. The code I have is too lengthly, as the arrays will potentially be far longer. I tried playing with .each() and for loops but it get's very messy and won't compare every array.

var solution=[
[0,0,0],
[0,0,1],
[0,0,1]];

var value=[
[0,0,0],
[0,0,1],
[0,0,1]];

//compare arrays
    if (solution[0][0]==value[0][0] &&
        solution[0][1]==value[0][1] &&
        solution[0][2]==value[0][2] &&
        solution[1][0]==value[1][0] &&
        solution[1][1]==value[1][1] &&
        solution[1][2]==value[1][2] &&
        solution[2][0]==value[2][0] &&
        solution[2][1]==value[2][1] &&
        solution[2][2]==value[2][2]) { 

        $('h1').show();

    }
    else { $('h1').hide();}

2 Answers 2

9

simple trick, by making them into strings :)

function equalArray(a, b) {
    return JSON.stringify(a) == JSON.stringify(b);
}
Sign up to request clarification or add additional context in comments.

Comments

-2

You can compare arrays using

 $(solution).compare(value)

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.