Shreedhar
Shreedhar

Reputation: 5640

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?

Upvotes: 6

Views: 3082

Answers (8)

Jasper de Vries
Jasper de Vries

Reputation: 20188

If you know what (not) to expect in your array you could use join:

a.join() == b.join()

I know, this is far from bulletproof, but it can be usable some cases (when you know the order in both arrays will be the same).

Upvotes: 6

KooiInc
KooiInc

Reputation: 122906

If you want to compare 2 arrays, you could use JSON.stringify

JSON.stringify([1,2,3]) === JSON.stringify([1,2,3]); //=> true

It will also compare [nested] Objects within the array, or [nested] Arrays within an Array:

JSON.stringify([1,2,3,{a:1,b:2}]) === 
  JSON.stringify([1,2,3,{'a':1,b:2}]); //=> true

JSON.stringify([1,2,3,{a:1,b:2,c:{a:1,b:2}}]) === 
  JSON.stringify([1,2,3,{'a':1,b:2,c:{a:1,b:2}}]); //=> true

JSON.stringify([1,2,3,{a:1,b:2,c:[4,5,6,[7,8,9]]}]) === 
  JSON.stringify([1,2,3,{'a':1,b:2,c:[4,5,6,[7,8,9]]}]); //=> true

In this jsfiddle, I've played a bit with the idea

Upvotes: 2

Alexandre Khoury
Alexandre Khoury

Reputation: 4022

It's the shortest way I found:

""+ar1==""+ar2

Fiddle

Upvotes: 0

alexandernst
alexandernst

Reputation: 15109

You have 2 options.

Fisrt one is to use some kind of function made by yourself that will iterate over each key from both arrays and compare them.

Second option is to use isEqual from _.underscore (a really nice JS library, http://underscorejs.org/#isEqual ) This will work for both arrays and objects.

I'd use the second one as it's easier.

var a = {'a' : '1', 'b' : '2', 'c' : '3'};
var b = {'a' : '1', 'b' : '2', 'c' : '3'};
_.isEqual(a, b) // --> true

Note that order doesn't matter in objects, so you could have

var a = {'a' : '1', 'b' : '2', 'c' : '3'};
var b = {'c' : '3', 'b' : '2', 'a' : '1'}
_.isEqual(a, b) // --> also true

Upvotes: 6

Chase
Chase

Reputation: 29549

Try using javascripts Join() method to convert the two arrays to strings and then compage the strings:

Join(): The string conversions of all array elements are joined into one string.

var a1 = array1.join();
var a2 = array2.join();

if(a1 == a2){
  //do something
}

Upvotes: 1

David W
David W

Reputation: 10184

You must write code to compare each element of an array to accomplish your objective.

// this is one way of doing it, and there are caveats about using instanceOf. 
// Its just one example, and presumes primitive types.
function areArrayElementsEqual(a1, a2)
{
    if (a1 instanceof Array) && (a2 instanceof Array)
    {
        if (a1.length!=a2.length)
            return false;
        else{
           var x;
           for (x=0;x<a1.length; x++)
              if (a1[x]!=a2[x])
                 return false;
        }
    }

    return true;

}

Upvotes: 1

Trevor
Trevor

Reputation: 6689

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

Upvotes: 6

3on
3on

Reputation: 6339

(a==b) is doing a reference comparaison not a content comparaison.

Underscore.js brings some feature for that.

Upvotes: 2

Related Questions