0
var arrays = [[1, 2]],
        array = [1, 2],   
        index = arrays.indexOf(array);

console.log(index);

In the code above, the value of index is -1, indicating that the array [1,2] could not be found inside of [[1,2]]. The method works fine for one-dimensional arrays, but why doesn't it seem to work for multi-dimensional arrays? or am I doing something wrong?

photon
  • 606
  • 4
  • 14
  • 31
  • 3
    It will compare object references, which are not the same for your arrays. In this case, you have to implement the search yourself. – Sirko Jul 19 '15 at 08:15
  • 1
    Objects (incl. Arrays) are compared by **reference**, not by values. So: `[1, 2] != [1, 2]` – hindmost Jul 19 '15 at 08:16

0 Answers0