0

Here is a simple example of an array that contains at least one other array. I want a way to find the index of an array, within an array. However, the code I have below does not work:

var arr = [1,2,[1,2]];
console.log(arr.indexOf([1,2])); //-> -1

for (var i = 0; i < arr.length; i++) {
  if (arr[i] == [1,2])
    return 'true' // does not return true
}

Intuitively, this should work but does not:

if ([1,2] == [1,2]) return 'true' // does not return true

Can someone explain why it does not work, and offer an alternative solution? Thanks!

9
  • what result do you expect? Commented Oct 5, 2015 at 17:11
  • 4
    in JS arrays are compared by reference. that's why [1,2] != [1,2] Commented Oct 5, 2015 at 17:13
  • 3
    The first [1,2] is not the same as the second [1,2]: they are different objects. Commented Oct 5, 2015 at 17:13
  • 2
    Possible duplicate of How to check if two arrays are equal with JavaScript? Commented Oct 5, 2015 at 17:15
  • possible duplicate of How to compare arrays in JavaScript? Commented Oct 5, 2015 at 17:17

3 Answers 3

1

No, but you can check it yourself:

var a = [1,2], b = [1,2];

a.length === b.length && a.every(function(x,y) { return x === b[y]; });
Sign up to request clarification or add additional context in comments.

Comments

0

Arrays in JavaScript are compared by reference not by value. That is why

console.log([1,2] == [1,2])

returns false.

You need a custom function that could compare arrays. If you want to check only the first level of nesting you can use this code:

var compareArrays = function(a, b) {
    if (a.length !== b.length) {
        return false;
    }

    for (var i = 0, len = a.length; i < len; i++) {
        if (a[i] !== b[i]) {
            return false;
        }
    }

    return true;
}

Comments

0

You are confusing the definitions of similar objects vs. the same object. Array.prototype.indexOf() compares the objects using the strict equality comparison algorithm. For (many) objects, this means an object reference comparison (i.e. the object's unique identifier, almost like its memory address).

In your example above, you are trying to treat two similar arrays as though they were the same array, and that's why it doesn't work. To do what you are trying to do, you need to use the same array. Like this:

var testArr = [1,2]; //a specific object with a specific reference
var arr = [1,2,testArr]; //the same array, not a different but similar array
console.log(arr.indexOf(testArr)); //returns 2

If you just want to know where arrays occur in the parent array, use Array.isArray():

...
if(Array.isArray(arr[i])) {
    //do something
}
...

Hopefully that helps!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.