Multidimensional arrays in javaScript
In JavaScript Multidimensional arrays can be implemented in a number of ways. Maybe the most common way is to just have arrays of arrays, however there are other ways of doing so that involve just having a single linear array and a formula to get or set the proper index value in the javaScript array. In addition there is also doing things like having an array of arrays, bit each element is an object and these objects then have an array as one of its properties.
Multidimensional arrays will come up often when it comes to any kind of project that will involve a 2d grid, a 3d plane, tables, or anything else where doing so might be called for when doing something interesting with many dimensions. So having a solid understanding of the different ways to go about having arrays with two or more dimensions is called for when it comes to a wide range of applications where the use of them will come into play. So lets look at some typical examples, as well as some other examples that might be not so typical with multidimensional arrays in javaScript.
1 - Using Arrays of arrays
The most common way of making a multidimensional array in javaScript might be to just have arrays of arrays. That is having a single array, and then have each element in that array be an array. That alone would be a 2d array of arrays at which point making each element in each nested array an array would add yet another dimension, and so on.
In this section I will be going over just some simple examples of making 2d arrays of arrays this way.
1.1 - Using literal array bracket syntax to create a static array of arrays
So for this example I just have a grid of single digit hex values that is four by four. I start out with just a simple array, and then have four arrays within that array. In each nested array I then have my hex values where the lowest hex value is the first index of the first nested array, things then progress from that index value forward to the end of the nested array, and then continue in the next.
I can then access the primitive value in each nested array, by using the bracket syntax just like a single array, only when doing so I get a nested array rather than a primitive value, so Then need to give the index value of the element that I want in the nested array to get an element with one of my hex values.
If you are still confused maybe it would be best to look at a code example of this.
|
|
This is a static form of a 2d array of arrays, however often I will want to have a way to generate such an array, so for this I will want to use some loops, and maybe do so in the body of a function.
1.2 - Using a function that will generate an array of arrays
I almost all projects I will want a way to go about creating a multidimensional array by way of some javaScript code that will generate the state of the array rather than the state being some kind of fixed hard coded value.
|
|
2 - Using just one linear array
Although arrays of arrays work okay, I often just use a single array, and then work out a formula that is used to figure the proper index value for this single array.
2.1 - A static example of using a single array and formula to get the proper index value
One again lets start out with a simple static hard coded form of this kind of multinational array in javaScript.
|
|
2.2 - Making a function that will return an API with a single array and get method
Now to do the same thing, only once again make a function that will return my multidimensional array. Only this time it will return the array along with at least a width property. In addition I might also want a height property, and a method that I can use to get a location by way of passing an x and y value.
|
|
3 - Three or more dimensions
So then there is working something out for working with three or more dimensions. I quickly put something together for that, but it is still not working the way I would like it to really.
|
|