Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comparison of array in javascript

I have two arrays say a = [1,2,3] and b=[1,2,3]

if i do (a==b) it returns false. how to compare two arrays with same values?

a[0]==b[0] will return true, but how can we compare two arrays instead of 2 same elements inside two different arrays?

like image 611
Shreedhar Avatar asked Sep 13 '12 18:09

Shreedhar


1 Answers

function array_compare(a, b)
{
    // if lengths are different, arrays aren't equal
    if(a.length != b.length)
       return false;

    for(i = 0; i < a.length; i++)
       if(a[i] != b[i])
          return false;

    return true;
}
like image 181
Trevor Avatar answered Oct 05 '22 18:10

Trevor