1

I'm learning Javascript and would like to understand why indexOf isn't finding the array within the searched array. (An explanation, theory etc. is the goal for this one, no alternative code or other solutions are needed)

In looking for the array [1, 2] within the array "arr" - why does indexOf not see the array?

Does this have to do with values vs.

const o = { name: "Ethan" };
const arr = [1, 5, "a", o, true, 5, [1, 2], "9"];
console.log(arr.indexOf([1, 2])); //returns -1 expected 1

Expected a return of 1, instead received -1. Why? Anyone able to share the details/theory/computer science aspect of why this is the case?

The answer at: indexOf( object) in javascript addresses objects, not arrays

The answer at: Looking for a good clear explanation for why this doesn't work addresses functions and for loops, not the indexOf method

The answer at: indexOf method in an object array? addresses objects and offers solutions to change how its being done, however I don't see an explanation as to why two arrays that appear to be identical are not being identified as being identical.

The reference of: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Using_the_Equality_Operators addresses comparison operators - and perhaps the answer lies within the realm of strict equality. And this may be where the lack of understanding on my part is. Is the [1, 2] that is being searched for somehow being construed as different than the one that is in the array 'arr' ?

The reference of: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf#Description addresses indexOf but doesn't appear to share any guidance on an array within another array. (Jagged array?)

I'm new to Javascript (as you can tell) and am looking to understand in detail why and/or how in the example the search for [1, 2] doesn't match the element of the arr array that is also [1, 2]

Melongro
  • 15
  • 4

3 Answers3

2

Because

indexOf() compares searchElement to elements of the Array using strict equality (the same method used by the === or triple-equals operator).

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf#Description

and

Two distinct objects are never equal for either strict or abstract comparisons. An expression comparing Objects is only true if the operands reference the same Object.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Using_the_Equality_Operators

Scott Weaver
  • 7,192
  • 2
  • 31
  • 43
  • In researching this further... 1.) arrays in JavaScript actually are objects, even though they are written differently(https://stackoverflow.com/questions/5048371/are-javascript-arrays-primitives-strings-objects) 2.) Since arrays are objects, each array exists independently. indexOf is looking for exact matches, and although the arrays contain the same values, the indexOf method is not looking at the values but the array object as a whole. Is this accurate on why the arrays don't match? – Melongro Jul 20 '19 at 20:20
  • yes, that's accurate. For example, two people named Melongro wouldn't necessarily be the same person :) – Scott Weaver Jul 20 '19 at 20:23
0

Whenever you compare two array using == or === it checks whether the both operands refer to the same object(array) in the memory. indexOf method internally uses === operator.

The array you pass to the function doesn't not refer to array which is element of another array although their elements are same.

Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
0

You could use Array#indexOf with the same array of take another approach with Array#findIndex and check if the item is an array, the length and if each value is equal.

const
    o = { name: "Ethan" },
    a = [1, 2],
    arr = [1, 5, "a", o, true, 5, [1, 2], a, "9"];

console.log(arr.indexOf(a));       // 7
console.log(arr.findIndex(item =>
    Array.isArray(item) &&
    item.length === a.length &&
    a.every((v, i) => a[i] === v)
));                                // 6
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392