我想我在這裏誤解了一些東西 - 我通常在PHP中工作,並認爲我錯過了一些小東西。我的最後一個數組tmp是空的,並顯示爲「,,,,,,,,,,,,,,,,」。在我看來,我的tmp數組可能在某處被清空,或者由於某種原因,範圍被重置。我使用它作爲表格中的座標,您可以選擇表格行併發布到web服務,但我的數組似乎是錯誤的。Javascript多維數組爲空
var length = $("#arrayCount").html();
var letters = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
var col = getSelectedColumn(); //for example sake lets say "B" is the selected column
var row = getSelectedRow(); //selected rows will be from "11" - "16"
var columnIndexStart = letters.indexOf(col[0]);
var tmp = [];
for(var i = row[0]; i <= row[1]; i++) //rows[0] = 11 and rows[1] = 16
{
tmp[i] = [];
for(var j = columnIndexStart; j < letters.length; j++) //columns and starts at index 1 if we work with "B"
{
var val = $("#" + i + "_" + letters[j]).html(); //using the row and letters as the associated DOM elements ID. Easier to retrieve it's HTML then.
if(val != undefined)
{
console.log("Index [" + i + "]['" + letters[j] + "'] = " + val); //works perfectly and prints as it should.
tmp[i]['"'+letters[j]+'"'] = val; //using quotes to save letters? Is this preferred?
}
}
}
console.log('Final Array: ' + tmp); //empty??
console.log('Final Array: ' + tmp[14]['G']); //testing HTML output. But is undefined.
return tmp;
你確定getSelectedColumn和getSelectedRow工作正常嗎? – GiuServ
添加輸出示例。 – Muppet
只是一個例子嗎?我的意思是。 index [14] ['G']是否存在? – GiuServ