arrays - javascript indexOf() keeps returning -1 -


i have array this

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

i running simple line of indexof(). however, keep getting -1 although test value i'm using exist in array.

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

did make mistake somewhere ?

you can use #findindex() , #every() find index of inner array inside outer array - see demo below:

var = [[1, 0], [3, 0], [5, 0], [7, 0]];  var search = [1,0]; // array index of 'a'    var = a.findindex(function(n) {    return search.every(function(p,q) {       return p === n[q]    });  });  console.log("index: ", i);

and simpler es6 flavor:

var = [[1, 0], [3, 0], [5, 0], [7, 0]];  var search = [1, 0]; // array index of 'a'    var = a.findindex(n => search.every((p, q) => p === n[q]));  console.log("index: ", i);


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -