Reputation: 81
I am aware that javascript does not allow true multidimensional arrays but you can sort of "bodge" one together by having arrays within arrays.
I have 2 questions;
1) Would converting 10 arrays into 1 multidimensional array which has 10 arrays within it improve, damage or have no effect on the performance of the webpage?
2) I am hoping to create an array that has 100 elements, each of these will be an array of 2 elements and finally each of these will be an array of indeterminate length.
I have the following code to create this "multidimensional" array:
var RouteArray = new Array(100);
for (var i=0; i <100; i++)
{
RouteArray[i]=new Array(2);
for (var j=0; j <2; j++)
{
RouteArray[i][j] = new Array();
}
}
Is this code sensible? or would you experienced coders be able to suggest a better method?
Also I assume that I could then add to the indeterminate arrays in the multidimensional array described above with the following code:
RouteArray[88][0].push(ValueToPush);
finally could I then call the value I have just pushed using the following?
Var ValueAdded = RouteArray[88][0][0];
Thanks in advance for your time, and apologies if this question has been answered elsewhere, I did try to search but either couldn't understand the previous posts or found them to be so unlike my situation that I couldn't apply what they suggested.
John
Upvotes: 3
Views: 280
Reputation: 437336
1) Would converting 10 arrays into 1 multidimensional array which has 10 arrays within it improve, damage or have no effect on the performance of the webpage?
Almost certainly no performance effect, but it will have a huge effect on your ability to understand the code and write less bugs.
2) I am hoping to create an array that has 100 indices, each of these will be filled with an array of 2 indices and finally each of these 2 indices will have an array of indeterminate length.
That will work exactly like your example code outlines, and it's a good starting point if you want to get your feet wet. In practice arrays with many dimensions are not always the best data structure to model whatever it is your code works with, mainly because they are not "discoverable": you have to know what each dimension is beforehand.
It's quite possible that there is a better way to structure your data in any one particular scenario, but we 'd have to be talking concretely. In any case, the code you give will work.
Upvotes: 3