JavaScript Multi-Dimensional Array
Introduction
The JavaScript Reference does not mention anything about a multi-dimensional array. Hey, you can create one. I discovered how, and I show you the way in this article.
You need to already know the meaning of one and two-dimensional arrays in order to understand this article.
Note: If you cannot see the code or if you think anything is missing in this article (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
The One Dimensional JavaScript Array
The JavaScript reference specifies a one-dimensional array. Normally each element of the one-dimensional array is a literal (value). Now, here is the secret for a two-dimensional array: If you have a one-dimensional array and you make each element of the one-dimensional array, a new array, then you would have a two dimensional array.
Creating a Two-Dimensional JavaScript Array
You start by creating a one-dimensional array as follows:
twoDArr = new Array();
Assume that you want a two-dimensional array of five rows; you would make five elements of the twoDArr array, new one-dimensional arrays, using the new operator. You do this:
twoDArr[0] = new Array();
twoDArr[1] = new Array();
twoDArr[2] = new Array();
twoDArr[3] = new Array();
twoDArr[4] = new Array();
With this you have a two-dimensional array. It is a rather long process compared to what you have in other languages, but you finally got your two dimensional array.
Note: With JavaScript you do not have to decide on the length of a one or two-dimensional array. So the above two-dimensional array code is OK. If you know the number of rows and if they are many, then assigning a new one-dimensional array for each row as done above would be tedious. You solve this problem by assigning the new one-dimensional arrays in a for-loop. In this light, the above two-dimensional array is created as follows:
twoDArr = new Array();
for (i=0; i {
twoDArr[i] = new Array();
}
Accessing Values
You access the value of a JavaScript 2D array as follows:
ArrayName[i][j]
where i is the row number and j is the column number. Row and column counting begins from zero.
So to access the value in row 2, column 4, for the above array, you would type,
twoDArr[1][3]
Example Code
Try the following code:
twoDArr = new Array();
twoDArr[0] = new Array();
twoDArr[1] = new Array();
twoDArr[2] = new Array();
twoDArr[3] = new Array();
twoDArr[4] = new Array();
twoDArr[2][3] = “two, three”;
alert(twoDArr[2][3]);
Multi-Dimensional JavaScript Array
A two-dimensional array is already a multi-dimensional array. Well, the word, “multi”, refers to two or more dimensions. In this section and till the end of the chapter, we shall talk about three and more dimensions.
Three Dimensional Regular JavaScript Array
You create a multidimensional array in the same way that you create a two dimensional array, by assigning new arrays as elements to a previously created array.
The following code creates a regular three-dimensional array. Try it and then see the explanation below.
//create one-dimensional array
threeDArr = new Array();
//create two-dimensional array
threeDArr[0] = new Array();
threeDArr[1] = new Array();
threeDArr[2] = new Array();
//create three-dimensional array
threeDArr[0][0] = new Array();
threeDArr[0][1] = new Array();
threeDArr[0][2] = new Array();
threeDArr[1][0] = new Array();
threeDArr[1][1] = new Array();
threeDArr[1][2] = new Array();
threeDArr[2][0] = new Array();
threeDArr[2][1] = new Array();
threeDArr[2][2] = new Array();
threeDArr[2][0][1] = “two, one, three”;
alert(threeDArr[2][0][1]);
The first code segment is just one statement; it creates a one-dimensional array. The next code segment creates a two-dimensional array by assigning a new one-dimensional array to each element of the previously created one-dimensional array. This is how you create a multi-dimensional array in JavaScript, by assigning a new array to any element of a previously created array.
To have a three dimensional array, you assign new arrays to the elements of the arrays you last created. That is what the third code segment above does.
The fourth code segment is a single statement. It assigns a value to one of the cells of the three dimensional array. The last code segment, which is also a single statement, displays the assigned value.
The above array is a regular three-dimensional array of height 3, width 3 and depth 3. It would have been more convenient to create this particular array using for-loops as in the following code.
//create one dimensional array
threeDArr = new Array();
//use for-loops to create three-dimensional array
for (i=0; i {
threeDArr[i] = new Array();
for (j=0; j {
threeDArr[j] = new Array();
for (k=0; k {
threeDArr[k] = new Array();
}
}
}
threeDArr[2][0][1] = “two, one, three”;
alert(threeDArr[2][0][1]);
You can try the above code.
Regular Multi-Dimensional Array
In the above code samples, each of the elements of a previously created array has a new array. This would always lead to a regular multi-dimensional array. A regular multi-dimensional array does not always need to have the height, width and depth equal. However, the “walls” should be plain. For a regular multi-dimensional array, the number of elements for the last set of arrays created should be the same.
Irregular Multi-Dimensional Array
A multi-dimensional array, whereby some elements of a previously created array have literals and others have new arrays, is an irregular multi-dimensional array. You still end up with an irregular multi-dimensional array if all the previously created arrays have new arrays for all their elements, but the last set of arrays created do not have the same number of elements.
You cannot create irregular multi-dimensional arrays using for-loops as in the above cases. You generally would create such an array by assigning new arrays to some elements of a previously created array, one-by-one.
Read and try the following code, which illustrates an irregular multi-dimensional array.
//create one-dimensional array
threeDArr = new Array();
//create two dimensional array
threeDArr[0] = new Array();
threeDArr[1] = new Array();
threeDArr[2] = “two”;
//create three-dimensional array
threeDArr[0][0] = new Array();
threeDArr[0][1] = new Array();
threeDArr[1][0] = new Array();
threeDArr[1][1] = new Array();
threeDArr[1][2] = “one two”;
alert(threeDArr[1][2]);
Four or More Dimensions
You create four or more dimensional arrays in a similar way as you create three-dimensional arrays.
The Cell
You access a cell by using the array names with indices in square brackets. The last assigned arrays are what actually have the cells. In a regular array, no previously created array has a cell; here the element of any previously created array has but an array.
That is what I prepared, for multi-dimensional arrays.
- Loulith Galenzoga
Related Posts
No related posts.