1

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.

MaxAx
  • 101
  • 2
  • 12
  • There are no multi-dimensional arrays in JavaScript. Use an array of arrays instead. – Tomalak May 14 '14 at 14:32
  • 1
    Your problem comes from the comma operator that gets applied to your two indices (Javascript only supports one). See [JavaScript multidimensional array](http://stackoverflow.com/questions/7545641/javascript-multidimensional-array). – Frédéric Hamidi May 14 '14 at 14:32
  • i think syntex is wrong – ashishmaurya May 14 '14 at 14:33
  • @user, on the contrary, the syntax is valid, but `imgArray[1, 0]` is evaluated as `1; imgArray[0]` because of the comma operator. – Frédéric Hamidi May 14 '14 at 14:34
  • @FrédéricHamidi this sounds interesting, can you tell us more about what is happening here and how is this syntaxically correct? – GôTô May 14 '14 at 14:37
  • @GôTô, yes. The [comma operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator) evaluates its operands from left to right and returns the value of its last operand. Only a single array index is supported, so `imgArray[1, 0]` applies the comma operator to `1` and `0` and uses the result (`0`) as the array index. There could be anything instead of `1`, it would not matter (to be specific, only the side effects of that expression would matter). – Frédéric Hamidi May 14 '14 at 14:39
  • @FrédéricHamidi thank you, sounds very useful (and quite obscure) to understand js. Got burnt by visiting your wife's site btw – GôTô May 14 '14 at 14:46

2 Answers2

5

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";
nils
  • 25,734
  • 5
  • 70
  • 79
  • Tried code change above, get undefined error when trying to access imgArray[1,2]. I have tried every example in all the previous posts about this same topic and they all ignore my left dimension. – MaxAx May 14 '14 at 14:50
  • Don't try to get `imgArray[1,2]` (read the answers instead). The correct use is `imgArray[1][2]` – nils May 14 '14 at 14:51
  • That worked. After so many hours looking at this BS, I guess I overlooked the details in the comments. Thank you very much. – MaxAx May 14 '14 at 14:59
2
imgArray = [];
imgArray[0] = ["image1", "image2"];

Multidimensional arrays in JavaScript are simply nested arrays.

lucassp
  • 4,133
  • 3
  • 27
  • 36
  • Tried this imgArray[0]=["image1.jpg","image3.jpg","image5.jpg"]; imgArray[1]=["image2.jpg","images4.jpg","image6.jpg"]; and for imgArray[0,1] I get returned everything in 1,0 which is image2, image4, image6. – MaxAx May 14 '14 at 14:54