我有一个2D array
,如下所示:
var numbers=[[1,2,3,4,5],[6,2,3,5,5],[9,8,3,4,9]]
如何找到上述二维数组的索引值?
答案 0 :(得分:0)
试试这个
var searchItem=2;
numbers.forEach(function(parentItem,parentIndex){
parentItem.forEach(function(childItem,childIndex){
if(childItem===searchItem){
console.log(parentIndex);
console.log(childIndex);
}
})
});
答案 1 :(得分: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;
}
测试:
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);
发现2次出现:0 * 3,2 * 3