0

Have been bug testing for about 2 hours now only to realise that if two arrays are the same. The logical operator == and === say it isn't.

Firstly, why. Secondly, how can I get this to be true?

Mathias
  • 73
  • 1
  • 8

2 Answers2

1

Array is a reference data type, and can never be equal. You can check and compare the data inside both arrays, without shell

Yuliia
  • 21
  • 2
0

Try this:

JSON.stringify(array1) == JSON.stringify(array2)

You're using logical comparisons between values, not for arrays/objects. Alternatively you can:

if (array1.length == array2.length){
    for(var i = 0; i <= array1.length; i++){
        if(array1[i] != array2[i]){
            return false;
        }
    }
    return true;
} else {
    return false;
}
bartholomew
  • 102
  • 8