-2

I have a 2D array which looks like this:

var numbers=[[1,2,3,4,5],[6,2,3,5,5],[9,8,3,4,9]]

How to find the index value of the above two dimensional array?

Vivek Todi
  • 361
  • 1
  • 9
  • 24

2 Answers2

0

Try like this

var searchItem=2;
numbers.forEach(function(parentItem,parentIndex){
  parentItem.forEach(function(childItem,childIndex){
     if(childItem===searchItem){
        console.log(parentIndex);
        console.log(childIndex);            
     }     
 })

});
Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
0
function findInArr(arr, elm) {
    var occ = [];
    for(var i = 0; i < arr.length; i++)
        for(var j = 0; j < arr[i].length; j++)
            if(arr[i][j] == elm)
                occ.push(i+"*"+j);
    return occ;
}

Test:

var numbers = [[1,2,3,4,5],[6,2,3,5,5],[9,8,3,4,9]];
var x = findInArr(numbers, 4);
console.log("found " + x.length + " occurences: " + x);

found 2 occurences: 0*3,2*3

Samurai
  • 3,724
  • 5
  • 27
  • 39