对于JavaScript中数组中的数组(如下所示), 为什么我不能这样循环?
for (var i = 0, len= animation.length;i <len; i++) {
for ( h = 0, len2= animation[i].length;h <len2; h++) {
animation[i][h][0]=animation[i][h][0]*30;
animation[i][h][1]=animation[i][h][1]*30;
}
}
document.write(animation);
EDIT))))))))))))))))))))))))) 原因?因为我无意中使我的json数据变成了var = animation;字符串,一个看起来像数组的字符串。感谢帮助,对不起麻烦
示例'多维'数组:
[
[
[17, 28, 1, "z"],
[18, 55, 2, "r"],
[11, 36, 2, "r"],
[18, 39, 2, "r"],
[18, 39, 2, "r"],
[24, 36, 2, "r"],
[18, 36, 2, "r"],
[23, 36, 2, "r"],
[12, 35, 2, "r"],
[24, 34, 2, "r"],
[18, 27, 8, "r"],
[17, 28, 4, "r"],
[18, 2, 6, "b"],
[24, 15, 6, "b"],
[18, 15, 6, "b"],
[18, 15, 6, "b"],
[12, 15, 6, "b"],
[24, 21, 6, "b"],
[17, 21, 6, "b"],
[18, 24, 6, "b"],
[18, 24, 6, "b"],
[28, 31, 6, "b"],
[8, 29, 6, "b"]
],
[
[19, 28, 1, "z"],
[18, 56, 2, "r"],
[11, 35, 2, "r"],
[18, 38, 2, "r"],
[18, 38, 2, "r"],
[23, 35, 2, "r"],
[19, 35, 2, "r"],
[23, 35, 2, "r"],
[13, 35, 2, "r"],
[23, 35, 2, "r"],
[19, 28, 8, "r"],
[17, 27, 4, "r"],
[18, 1, 6, "b"],
[23, 16, 6, "b"],
[18, 16, 6, "b"],
[19, 16, 6, "b"],
[13, 16, 6, "b"],
[23, 22, 6, "b"],
[17, 22, 6, "b"],
[17, 25, 6, "b"],
[19, 25, 6, "b"],
[27, 32, 6, "b"],
[9, 28, 6, "b"]
],
[
[17, 26, 1, "z"],
[18, 56, 2, "r"],
[11, 35, 2, "r"],
[18, 37, 2, "r"],
[18, 37, 2, "r"],
[23, 35, 2, "r"],
[20, 35, 2, "r"],
[23, 35, 2, "r"],
[14, 35, 2, "r"],
[23, 35, 2, "r"],
[19, 28, 8, "r"],
[16, 26, 4, "r"],
[18, 1, 6, "b"],
[22, 17, 6, "b"],
[17, 17, 6, "b"],
[18, 17, 6, "b"],
[14, 17, 6, "b"],
[23, 23, 6, "b"],
[18, 23, 6, "b"],
[16, 26, 6, "b"],
[20, 26, 6, "b"],
[26, 33, 6, "b"],
[10, 28, 6, "b"]
],
[
[15, 24, 1, "z"],
[18, 56, 2, "r"],
[11, 34, 2, "r"],
[18, 36, 2, "r"],
[18, 36, 2, "r"],
[23, 34, 2, "r"],
[21, 34, 2, "r"],
[23, 34, 2, "r"],
[15, 34, 2, "r"],
[22, 34, 2, "r"],
[18, 27, 8, "r"],
[15, 25, 4, "r"],
[18, 1, 6, "b"],
[22, 18, 6, "b"],
[16, 18, 6, "b"],
[17, 18, 6, "b"],
[15, 18, 6, "b"],
[22, 24, 6, "b"],
[18, 24, 6, "b"],
[15, 27, 6, "b"],
[21, 27, 6, "b"],
[25, 34, 6, "b"],
[11, 28, 6, "b"]
],
[
[13, 22, 1, "z"],
[18, 56, 2, "r"],
[11, 33, 2, "r"],
[18, 35, 2, "r"],
[18, 35, 2, "r"],
[23, 33, 2, "r"],
[22, 33, 2, "r"],
[23, 33, 2, "r"],
[16, 33, 2, "r"],
[21, 33, 2, "r"],
[17, 26, 8, "r"],
[14, 24, 4, "r"],
[18, 1, 6, "b"],
[21, 19, 6, "b"],
[17, 19, 6, "b"],
[16, 19, 6, "b"],
[16, 19, 6, "b"],
[22, 25, 6, "b"],
[19, 25, 6, "b"],
[14, 28, 6, "b"],
[22, 28, 6, "b"],
[24, 35, 6, "b"],
[12, 28, 6, "b"]
],
[
[11, 22, 1, "z"],
[18, 56, 2, "r"],
[10, 34, 2, "r"],
[18, 34, 2, "r"],
[18, 34, 2, "r"],
[23, 34, 2, "r"],
[23, 34, 2, "r"],
[23, 34, 2, "r"],
[17, 34, 2, "r"],
[20, 32, 2, "r"],
[16, 25, 8, "r"],
[13, 23, 4, "r"],
[18, 1, 6, "b"],
[20, 20, 6, "b"],
[18, 18, 6, "b"],
[17, 20, 6, "b"],
[15, 20, 6, "b"],
[21, 26, 6, "b"],
[18, 25, 6, "b"],
[13, 29, 6, "b"],
[23, 29, 6, "b"],
[23, 34, 6, "b"],
[13, 28, 6, "b"]
],
[
[11, 22, 1, "z"],
[18, 56, 2, "r"],
[9, 33, 2, "r"],
[18, 33, 2, "r"],
[18, 33, 2, "r"],
[23, 33, 2, "r"],
[23, 33, 2, "r"],
[23, 33, 2, "r"],
[18, 33, 2, "r"],
[19, 31, 2, "r"],
[15, 24, 8, "r"],
[12, 22, 4, "r"],
[18, 1, 6, "b"],
[19, 21, 6, "b"],
[17, 19, 6, "b"],
[16, 21, 6, "b"],
[14, 21, 6, "b"],
[22, 27, 6, "b"],
[18, 26, 6, "b"],
[12, 30, 6, "b"],
[23, 30, 6, "b"],
[22, 33, 6, "b"],
[14, 28, 6, "b"]
],
[
[11, 22, 1, "z"],
[18, 56, 2, "r"],
[8, 34, 2, "r"],
[18, 32, 2, "r"],
[18, 32, 2, "r"],
[23, 34, 2, "r"],
[23, 34, 2, "r"],
[23, 34, 2, "r"],
[19, 34, 2, "r"],
[18, 30, 2, "r"],
[14, 23, 8, "r"],
[11, 22, 4, "r"],
[18, 1, 6, "b"],
[18, 22, 6, "b"],
[18, 18, 6, "b"],
[15, 22, 6, "b"],
[13, 22, 6, "b"],
[23, 28, 6, "b"],
[18, 27, 6, "b"],
[11, 31, 6, "b"],
[23, 31, 6, "b"],
[23, 34, 6, "b"],
[15, 28, 6, "b"]
],
[
[9, 20, 1, "z"],
[18, 56, 2, "r"],
[9, 33, 2, "r"],
[18, 31, 2, "r"],
[18, 31, 2, "r"],
[23, 33, 2, "r"],
[23, 33, 2, "r"],
[23, 33, 2, "r"],
[20, 33, 2, "r"],
[17, 29, 2, "r"],
[13, 22, 8, "r"],
[10, 21, 4, "r"],
[18, 1, 6, "b"],
[17, 23, 6, "b"],
[19, 19, 6, "b"],
[14, 22, 6, "b"],
[12, 21, 6, "b"],
[22, 29, 6, "b"],
[18, 28, 6, "b"],
[10, 32, 6, "b"],
[23, 32, 6, "b"],
[22, 33, 6, "b"],
[16, 28, 6, "b"]
],
[
[9, 20, 1, "z"],
[18, 56, 2, "r"],
[8, 34, 2, "r"],
[18, 30, 2, "r"],
[18, 30, 2, "r"],
[23, 34, 2, "r"],
[23, 34, 2, "r"],
[23, 34, 2, "r"],
[21, 34, 2, "r"],
[16, 28, 2, "r"],
[12, 21, 8, "r"],
[9, 20, 4, "r"],
[18, 1, 6, "b"],
[18, 24, 6, "b"],
[18, 18, 6, "b"],
[13, 21, 6, "b"],
[11, 20, 6, "b"],
[23, 30, 6, "b"],
[18, 29, 6, "b"],
[11, 31, 6, "b"],
[23, 33, 6, "b"],
[23, 34, 6, "b"],
[17, 28, 6, "b"]
],
[
[11, 22, 1, "z"],
[18, 56, 2, "r"],
[9, 33, 2, "r"],
[18, 29, 2, "r"],
[18, 29, 2, "r"],
[23, 33, 2, "r"],
[23, 33, 2, "r"],
[23, 33, 2, "r"],
[22, 33, 2, "r"],
[15, 27, 2, "r"],
[11, 20, 8, "r"],
[8, 20, 4, "r"],
[18, 1, 6, "b"],
[18, 25, 6, "b"],
[19, 18, 6, "b"],
[12, 20, 6, "b"],
[11, 20, 6, "b"],
[23, 31, 6, "b"],
[17, 28, 6, "b"],
[10, 32, 6, "b"],
[23, 33, 6, "b"],
[22, 33, 6, "b"],
[18, 28, 6, "b"]
]
]
答案 0 :(得分:3)
您对两个循环使用相同的长度变量。不能保证嵌套数组与外部数组具有相同的长度。看起来应该更像这样:
for (var i = 0, len= animation.length;i <len; i++) {
for ( h = 0, len2= animation[i].length;h <len2; h++) {
animation[i][h][0]=animation[i][h][0]*30;
animation[i][h][1]=animation[i][h][1]*30;
}
}
答案 1 :(得分:0)
发布代码时,请仅发布一个演示该问题的最小示例。此外,发布可以复制,粘贴和运行的完整代码,无需修改,否则您的问题可能会丢失在帖子的缺失部分。
以下显示您的代码已发布,有效。但没有什么可以将它与你发布的(过量)数据联系起来。
var animation =
[
[
[17, 28, 1, "z"],
[28, 31, 6, "b"],
[8, 29, 6, "b"]
],
[
[19, 28, 1, "z"],
[17, 25, 6, "b"],
[19, 25, 6, "b"],
[27, 32, 6, "b"],
[9, 28, 6, "b"]
]
]
for (var i = 0, len= animation.length;i <len; i++) {
for ( h = 0, len2= animation[i].length;h <len2; h++) {
animation[i][h][0]=animation[i][h][0]*30;
animation[i][h][1]=animation[i][h][1]*30;
}
}
document.write(animation);