Knowledge Base Articles » KB100213: Implementing Multi-dimensional Arrays in JavaScript.
Unlike VBScript, JavaScript does
not natively support multi-dimensional arrays. However, it is possible to simulate a multi-dimensional array
by creating an array of arrays. This brief article demonstrates the
technique.
The following code snippet creates a "2-dimensional" array and populates it
with two "rows" and three "columns" of data:
JavaScript code:
<script type="text/javascript">
// Create the array
var Room = new
Array();
Room[0] = new Array();
Room[1] = new Array();
// Populate the array
Room[0][0] = "white";
Room[0][1] = "red";
Room[0][2] = "blue";
Room[1][0] = "black";
Room[1][1] = "yellow";
Room[1][2] = "purple";
// Iterate the array
for (var i=0; i<2; i++)
for (var j=0; j<3; j++)
document.write(Room[i][j]
+ "<br>")
</script>
Browser output:
white
red
blue
black
yellow
purple
This concept is easily extended to simulate three- and
higher- dimensional arrays.