3

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
Leau
  • 1,072
  • 5
  • 19
Tukkan
  • 1,574
  • 2
  • 18
  • 33
  • 2
    This really helps. http://stackoverflow.com/questions/12604062/javascript-array-indexof-doesnt-search-objects – Near Mar 26 '13 at 00:53
  • 1
    It returns `-1` because `b` isn't in `a`. `[1, 1] == [1, 1]` is `false` in JavaScript. – Blender Mar 26 '13 at 00:54

3 Answers3

4

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

Community
  • 1
  • 1
Ben McCormick
  • 25,260
  • 12
  • 52
  • 71
1

indexOf returns -1 if it does not find a match in the array. Your array a does not contain element b.

EDIT:

To clarify on what @JonathanLonowski said, the reason there is not a match is because you are doing a strict comparison, comparing the references, not the values.

Codeman
  • 12,157
  • 10
  • 53
  • 91
  • And that is what I wrote. The question is why. We see that 'a' and 'b' contain [1,1] – Tukkan Mar 26 '13 at 00:52
  • 1
    @Tukkan Because each `[...]` is a `new Array()` and `!==` any other `Array`. They may be *similar*, especially to a human eye, but they are not the *same* or *equal*. – Jonathan Lonowski Mar 26 '13 at 00:53
  • And is there any other way to check it, and return true value – Tukkan Mar 26 '13 at 00:54
  • 1
    @Tukkan Either have `a` actually contain `b` so they share the reference -- `var b = [1,1], a = [b, [2,2]];` Or, you have to use a *deep*-comparison that will traverse the objects and compare the primitive values they contain. http://stackoverflow.com/q/1068834 has an example of this. Many unit test frameworks have implementations of it as well. Example: [QUnit](http://api.qunitjs.com/deepEqual/). – Jonathan Lonowski Mar 26 '13 at 00:59
0

<Array>.some method tests whether at least one array in the multi-dimensional array passes the test implemented by the provided function.

<Array>.every method tests whether all array items in the array pass the test implemented by the provided function.

These two methods combined make it possible to check if all the items of an array in the multidimensional one are worth those of that sought.

const checkArray = (arrays, array) => arrays.some(a => {
  return (a.length > array.length ? a : array).every((_, i) => a[i] === array[i]);
});

const arrays = [[0, 1], [2, 2], [0, 3, 2, 1]];

[
  [0, 1],          // true
  [2, 2],          // true
  [0, 3, 2, 1],    // true
  [1, 0, 3, 2, 1], // false
  [2, 2, 1],       // false
  [0, 0],          // false
  [1, 2],          // false
  [0, 1, 2]        // false
].forEach(array => {
  console.log(checkArray(arrays, array));
});
Leau
  • 1,072
  • 5
  • 19