Javascript多维数组返回错误的维度

时间:2014-05-14 14:30:25

标签: javascript multidimensional-array

我初始化一个数组:

    imgArray = [];
    imgArray[0,0] = "image1";
    imgArray[1,0] = "image2";
    imgArray[0,1] = "image3";
    imgArray[1,1] = "image4";
    imgArray[0,2] = "image5";
    imgArray[1,2] = "image6";

当我为imgArray [0,2]发出警报时,我得到了image6。当我对imgArray [0,1]发出警报时,我得到了image4。当我对imgArray [1,1]发出警报时,我得到的image4是正确的。

似乎imgArray完全无视我的0维。

2 个答案:

答案 0 :(得分:5)

Javascript中的多维数组用单独的括号写成:

imgArray = [];
imgArray[0] = [];
imgArray[1] = [];
imgArray[0][0] = "image1";
imgArray[1][0] = "image2";
imgArray[0][1] = "image3";
imgArray[1][1] = "image4";
imgArray[0][2] = "image5";
imgArray[1][2] = "image6";

答案 1 :(得分:2)

imgArray = [];
imgArray[0] = ["image1", "image2"];

JavaScript中的多维数组只是嵌套数组。