Search code examples
javascriptarraysangularjskineticjs

Matching contents of 2 arrays angularjs


I have 2 arrays of the following contents:

var answer = [[2,1],[1,1],[0,0]];
var selectedObject = [[1,1],[0,0],[2,1]];

I want to match the contents of both the arrays. _.Equals is not working for me in the above condition. As the contents being same are not in same position in array.

Is there any easy way to match the contents of above mentioned arrays.

Any demo code, example, or logic will be helpful.


Solution

  • Finally solved it. Using _.Equals and basic for loop. It was so simple.

    if(answerArray.length != selectedAnsArray.length)
    {
        //wrong answer
        return;
    }
    else
    {
        for(var x = 0; x < answerArray.length; x++)
        {
            for(var y = 0; y < selectedAnsArray.length; y++)
            {
                if(_.isEqual(answerArray[x],selectedAnsArray[y]))
                count++;
            }
        }
        if(count==answerArray.length)
        {
           //correct answer
           return;
        }
        else
        {
          //wrong answer
          return;
        }
    }