1

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);

編集)))))))))))))))))))))))) 理由は? うっかり var=animation; になったjsonデータを作成してしまったためです。文字列、配列のように見える文字列に。助けてくれてありがとう、お手数をおかけして申し訳ありません

サンプル「多次元」配列:

[
    [
        [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"]
    ]
]
4

2 に答える 2

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;
    }
}
于 2013-06-06T22:32:25.873 に答える
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);
于 2013-06-06T23:19:55.973 に答える