Javascript multidimensional array returning wrong dimension [duplicate]

I initialize an array as such:

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

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.

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

Answers:

Answer

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";
Answer
imgArray = [];
imgArray[0] = ["image1", "image2"];

Multidimensional arrays in JavaScript are simply nested arrays.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.