2

I have an array like this

var a = [[1, 0], [3, 0], [5, 0], [7, 0]];

I am running a very simple line of indexOf(). However, I keep getting -1 although the test value I'm using does exist in the array.

var i = a.indexOf([1, 0]);
console.log("INDEX: ", i);

Did I make a mistake somewhere ?

PTN
  • 1,658
  • 5
  • 24
  • 54

3 Answers3

2

You can use #findIndex() and #every() to find the index of the inner array inside the outer array - see demo below:

var a = [[1, 0], [3, 0], [5, 0], [7, 0]];
var search = [1,0]; // array to get index of from the 'a'

var i = a.findIndex(function(n) {
  return search.every(function(p,q) { 
    return p === n[q]
  });
});
console.log("INDEX: ", i);

And the simpler ES6 flavor:

var a = [[1, 0], [3, 0], [5, 0], [7, 0]];
var search = [1, 0]; // array to get index of from the 'a'

var i = a.findIndex(n => search.every((p, q) => p === n[q]));
console.log("INDEX: ", i);
kukkuz
  • 41,512
  • 6
  • 59
  • 95
1

As @Nisarg mentioned, when you pass indexOf with the array it checks for the reference instead of the values. So in order to get the correct indexOf you should loop manually for each array.

var a = [[1, 0], [3, 0], [5, 0], [7, 0]];

function arrayIndexOf(arr){
  for(var x=0;x<a.length;x++){
    if(arr[0]===a[x][0]&&arr[1]===a[x][1]){
      return x
    }
  }
}

console.log(arrayIndexOf([1,0]));
Frankusky
  • 1,040
  • 9
  • 18
1

The indexOf function does not compare the array by value. So in order to be able to compare the two arrays, you need a function. In this case, I have used the function @Tomáš Zato has created here.

Once you have that, you can iterate the source array and get the index of a matching array.

// Warn if overriding existing method
if(Array.prototype.equals)
    console.warn("Overriding existing Array.prototype.equals. Possible causes: New API defines the method, there's a framework conflict or you've got double inclusions in your code.");
// attach the .equals method to Array's prototype to call it on any array
Array.prototype.equals = function (array) {
    // if the other array is a falsy value, return
    if (!array)
        return false;

    // compare lengths - can save a lot of time 
    if (this.length != array.length)
        return false;

    for (var i = 0, l=this.length; i < l; i++) {
        // Check if we have nested arrays
        if (this[i] instanceof Array && array[i] instanceof Array) {
            // recurse into the nested arrays
            if (!this[i].equals(array[i]))
                return false;       
        }           
        else if (this[i] != array[i]) { 
            // Warning - two different object instances will never be equal: {x:20} != {x:20}
            return false;   
        }           
    }       
    return true;
}
// Hide method from for-in loops
Object.defineProperty(Array.prototype, "equals", {enumerable: false});

var a = [[1, 0], [3, 0], [5, 0], [7, 0]];
var index = -1;
for(var i = 0; i < a.length; i++) {
  if(a[i].equals([1,0])) {
    index = i;
  }
}

console.log(index);
Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55