0
var a = [[1,0],[0,1],[0,0]];

If I want to find the index position of [0,0]? How can that be done in JavaScript?

I checked a few places and indexOf function but it doesn't work with multidimensional arrays.

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
anahom
  • 69
  • 4
  • 2
    Duplicate of http://stackoverflow.com/q/8670345/2902660 – Pineda Jan 21 '17 at 12:06
  • 1
    You might want to read about references and values in JavaScript. **indexOf** and all other functions that operates on generic objects will do comparison by reference. Two boxes contain the same numbers, but they aren't the same box. – Bernard Jan 21 '17 at 12:10

3 Answers3

0

You can make a hashmap of the indices and access that

var a = [[1,0],[0,1],[0,0]],

b = a.reduce((a,c,i)=>{
  a[c] = i;
  return a;      
},{});

console.log('Index of [0,0] is ' +b[ [0,0] ] )
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • @Pineda run the code and look at what it logs – charlietfl Jan 21 '17 at 12:32
  • apologies, still trying to get my head around why this works. It's darn sweet though and have no idea as to why it's down voted – Pineda Jan 21 '17 at 12:51
  • @Pineda because the arrays are implicitly cast to string when used as object properties – charlietfl Jan 21 '17 at 12:52
  • Blimey, so a hashmap is effectively an object whose properties act as access keys that hold a certain value, in this case the index of their position. This would break if there were duplicates in the array though, since the index returned would that of the last instance of it in the array... – Pineda Jan 21 '17 at 13:04
  • 1
    All due respect, this is a *terrible* idea. It relies on the side-effect of turning an array into a string when you use the array as a property name, and thus is both inefficient and only useful for a very, very narrow range of use cases (this *is* one of them, but there are very few). At the very, very, very least, this answer needs an explanation of what it's doing, why it will fail in most cases, and why it works in this specific case. – T.J. Crowder Jan 21 '17 at 13:17
  • Some browsers don't support the `reduce` method use this [polyfill](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce). –  Jan 21 '17 at 13:18
  • @T.J.Crowder: By side-effect, are you talking in regards to functional purity or do you mean the **quirk** of the array implicitly being cast to string? I ask mainly because if it is the former, isn't it the case that for all inputs, it would output the the same hashmap? – Pineda Jan 21 '17 at 13:46
  • @Pineda: I'm not at all sure I understand your question. But for instance searching for `["a","a"]` in `[["a","b"],["a","b"],["a","a"],["a,a"]]` would return the wrong index with the above technique. – T.J. Crowder Jan 21 '17 at 13:52
  • @T.J.Crowder: I was thinking in terms of the term 'side-effect' in terms of functional programming and 'pure' functions. I do see how the solution is specific but hadn't seen a hashmap created this way before. – Pineda Jan 21 '17 at 13:57
  • @Pineda: Ah, I see; no, I wasn't using the term side-effect in the functional programming sense. – T.J. Crowder Jan 21 '17 at 14:01
  • @T.J.Crowder: ok, cool. Thanks for pointing out the cons of this solution – Pineda Jan 21 '17 at 14:02
0

You could use Array#some for outer iteration and Array#every for inner iteration and check the length and the items.

var array = [[1, 0], [0, 1], [0, 0]],
    find = [0, 0],
    index = -1;

array.some(function (a, i) {
    if (a.length === find.length && a.every(function (b, j) { return b === find[j]; })) {
        index = i;
        return true;
    }
});

console.log(index);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • 1
    You really should just dupehammer the question: http://stackoverflow.com/q/8670345/2902660 (I can't because I was silly enough to vote it as unclear early on). *(not my dv, btw -- unlike many of the other answers here :-) Yours is the only decent solution here )* – T.J. Crowder Jan 21 '17 at 12:38
0

Fast(in terms of performance) solution using while loop and Array.prototype.every() function:

var a = [[1,0],[0,1],[0,0]], len = a.length,
        search_item = [0,1], pos = -1;

while (len--) {
    if ((a[len].length === search_item.length) 
            && a[len].every(function(el, i){ return el == search_item[i]; })) {
        pos = len;
        break;
    }
}

console.log(pos);

Array.prototype.every()

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105