Javascript 2d array indexOf

前端 未结 9 1594
既然无缘
既然无缘 2020-11-28 12:03

I have a 2d array like this:

var arr = [[2,3],[5,8],[1,1],[0,9],[5,7]];

Each index stores an inner array containing the coordinates of some

9条回答
  •  醉梦人生
    2020-11-28 12:44

    Not a complete answer just a side note that may help.

    Use Lodash

    This method will get you the position of a value within a 2 dimensional array

    let a = [ [ 'bird' ], [ 'cat' ], [ 'dog' ], [ 'cow' ], [ 'bird' ] ];
    let b = _.findIndex(a, function(el) { return el[0] == 'cow'; });
    console.log(b);//answer is 3
    

    As mentioned earlier you need a nested loop for traversing through the array.

提交回复
热议问题