-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?

3
  • 3
    What is the "index value"? Commented May 11, 2015 at 9:26
  • 1
    array[0][1] would return "2" from the first "block" in the array. Why and how do you want to return an index? Commented May 11, 2015 at 9:26
  • does this help stackoverflow.com/questions/16102263/… Commented May 11, 2015 at 9:29

2 Answers 2

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);            
     }     
 })

});
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.