How to check if a multidimensional array has a specific array?

arrays, javascript

Solution

indexOf compares using strict equality (`===`). Your elements would have to be the exact same object.

so

var a = [1,1];

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

b.indexOf(a)// 0

because `a === a`

but

b.indexOf([1,1])// -1

because [1,1] is a different object than `a` so they're not strictly equal.

MDN Docs

To do what you want to do you'll need to do something more involved. You can loop over the values and use something like whats in this question's answers to do the comparison

Problem

How to check if `arrays` has `array`? ``` var arrays = [[1, 1], [2, 2]]; var array = [1,1]; [1, 1] === [1, 1]; // false arrays.includes(array); // false arrays.indexOf(array); // -1 ```

Original source

Related problems