0
在此腳本中遇到一些麻煩。它遍歷一個二維數組並將每個相應的索引添加到一起。所以基本上arr [0] [1] + arr [0] [2] + arr [0] [3] ... arr [1] [1] + arr [1] [3] ] ...等。在二維數組中動態創建索引
這第一個工作正常。所以我的邏輯是好的。我的問題在於,我無法動態創建索引。我不認爲推動會起作用,因爲我在這裏總結了數值。
var cat_stats_week_radar = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0]];
for (var i = 0; i < cat_stats_week.length; i++) {
for (var j = 0; j < cat_stats_week[0].length; j++) {
cat_stats_week_radar[0][j] += +(cat_stats_week[i][j]);
}
}
這一個不工作,我沒有得到一個錯誤,只是一堆NaN值的。
var cat_stats_week_radar = [[]];
for (var i = 0; i < cat_stats_week.length; i++) {
for (var j = 0; j < cat_stats_week[0].length; j++) {
cat_stats_week_radar[0][j] += +(cat_stats_week[i][j]);
}
}
這裏是我的工作的陣列。
陣列添加:
var cat_stats_week = [
[0,0,0,0,0,0,0,1,0,0,0,0,0,0],
[0,0,0,0,0,0,1,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,1,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,1,0],
[0,0,0,0,0,0,0,0,0,0,0,0,1,0],
[0,0,1,0,0,0,0,0,0,0,0,0,0,0]
];
結果數組:
var cat_stats_week_radar = [[0, 0, 1, 0, 0, 0, 2, 1, 0, 0, 0, 0, 2, 0]];
完美。謝謝! – aph107