如何在Javascript中获取2D数组中的单元格值
我有一个数字表,其中31列* 9行,我想从获取点功能获取单元格值!所以我想[1] [2]。对我来说重要的价值只是Y?如何将网格传递给现场功能?此外,任何有关性能的建议都会很棒,因为你可以看到它是巨大的阵列
var cols = 31;
var rows = 9;
var theGrid = new Array(cols);
var i;
var j;
//get the spot
function getTheSpot(j, i) {
// when the col met the row it create a spot
//that what i need to get
this.y = i;
this.x = j;
return i;
return j;
}
//create a grid for the numbers
function createGrid() {
// BELOW CREATES THE 2D ARRAY
for (var i = 0; i < cols; i++) {
theGrid[i] = new Array(rows);
}
for (var i = 0; i < cols; i++) {
for (var j = 0; j < rows; j++) {
theGrid[j][i] = new getTheSpot(j, i);
}
}
}
var s = getTheSpot(9, 2);
console.log (s);
答案 0 :(得分:0)
如果我理解你的需要,你可以像这样引用一个数组元素:
var your_array = [ 10, 11, 15, 8 ];
// Array indexes start at 0
// array[0] is the the first element
your_array_name[0] == 10
//=> true
// array[2] is the third element
your_array_name[2] == 15
//=> true
现在,在二维矩阵(数组内的数组)中,以下是:
var awesome_array = [
[ 0, 10, 15, 8 ],
[ 7, 21, 75, 9 ],
[ 5, 11, 88, 0 ]
];
// Remember the index starts at 0!
// First array, first element
awesome_array[0][0] == 0
//=> true
// Second array, fourth element
awesome_array[1][3] == 9
//=> true
在你的情况下,你(据说)有这种布局:
var greatest_array = [
[ "A", "B", "C", "D", "E", "F" ],
[ "B", "C", "D", "E", "F", "G" ],
[ "C", "D", "E", "F", "G", "H" ]
];
// Your desired "E" is on the second array (index 1), fourth line (index 3):
console.log(greatest_array[1][3]); //=> "E"
干杯!