本文介绍了JavaScript多维数组返回错误的维数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样初始化数组:

    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.

When I do an alert for imgArray[0,2], I get image6. When I do an alert for imgArray[0,1], I get image4. When I do an alert for imgArray[1,1], I get image4 which is correct.

看来imgArray完全忽略了我的0维.

It appears that the imgArray is totally ignoring my 0 dimension.

推荐答案

JavaScript中的多维数组用单独的括号括起来:

Multidimensional Arrays in Javascript are written in seperate brackets:

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

这篇关于JavaScript多维数组返回错误的维数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 10:19