I have a 2D array
which looks like this:
我有一个2D数组,看起来像这样:
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?
如何找到上述二维数组的索引值?
2 个解决方案
#1
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);
}
})
});
#2
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
发现2次出现:0 * 3,2 * 3
#1
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);
}
})
});
#2
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
发现2次出现:0 * 3,2 * 3