I have created this multidimensional array in javascript.
var arr = [];
arr[0] = [];
arr[0][0] = [];
arr[0][0][0] = [];
arr[0][0][0][0] = [];
and assigning values to the using this code
arr[0] = 1;
arr[0][0] = 2;
arr[0][0][0] = 3;
arr[0][0][0][0] = 4;
arr[0][0][0][0][0] = 5;
alert("arr ==> " + arr);
But it gives output as only 1, but the desired output is 1,2,3,4,5
When I do this alert(arr[0][0]); the desired output is 2 but it gives undefined.
Thanks for helping.
asked Feb 6, 2013 at 6:22
Yogesh Suthar
30.5k18 gold badges75 silver badges100 bronze badges
2 Answers 2
You're overwriting your values:
arr[0] = [];
...
arr[0] = 1; // this also blows away arr[0][0], arr[0][0][0], etc
so...
arr[0][0] = 1;
==
1[0] = 1;
What exactly are you trying to do?
answered Feb 6, 2013 at 6:24
user578895
Sign up to request clarification or add additional context in comments.
2 Comments
Yogesh Suthar
I want to set the values at zero position upto 5 dimension.
var arr = [];
arr[4] = [];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
arr[4] = 5;
alert("arr ==> " + arr );
I hope this will solve your problem.
Alerts as
arr ==> 1,2,3,4,5
Comments
default
1,2,3,4,5. That being said, JavaScript is not PHP. Chances are you are using the wrong data structure.