Similar to one-dimensional arrays, you access the data in multi- dimensional arrays in much the same way you assign data to multi- dimensional arrays
you can initialize an array like this:
myArray1 = new Array("blue", "red", "purple", "lime")
you can reference your array with javascript like this:
document.write(myArray1[0])
which will print "blue"
If you put in this: document.write(myArray)
javascript will print [""blue","red","purple","lime"} -- just like this.
For a two diminsional array you would just assign the value of one element
of the array another array
example:
MyArray1 = new Array("alpha", "beta", "gamma")
MyArray2 = new Array("eeny", "meeny", "miney", myArray1)
<SCRIPT LANGUAGE=JavaScript>
<!--
function BuildStirFryColorArray()
//This function creates and fills a two-dimensional array.
{
StirFry = new Array(5) // declare a regular array
StirFry[0] = new Array(2) // in the first element of our array, - make that another array.
StirFry[0][0] = "chicken"
StirFry[0][1] = "beef"
StirFry[0][2] = "brown"
StirFry[1] = new Array(2) // in the second element, - make that it's own array...
StirFry[1][0] = "bamboo shoots"
StirFry[1][1] = "bean sprouts"
StirFry[1][2] = "white"
StirFry[2] = new Array(2) //
StirFry[2][0] = "Baby corn"
StirFry[2][1] = "yellow squash"
StirFry[2][2] = "yellow"
StirFry[3] = new Array(2)
StirFry[3][0] = "pea pods"
StirFry[3][1] = "celery"
StirFry[3][2] = "green"
StirFry[4] = new Array(2)
StirFry[4][0] = "Soy sauce"
StirFry[4][1] = "brown sugar"
StirFry[4][2] = "brown"
}
// -->
</SCRIPT>
</HEAD>
<BODY>
<H1>Accessing Data in a Two-Dimensional Array</H1>
<P>Ready to create an array of stir-fry ingredients? Okay, here we go.
<P>...
<SCRIPT LANGUAGE=JavaScript>
<!-- .
/* This program calls the array-creation routine. */
function MixItUp()
{
}
// -->
</SCRIPT>
Access each of the elements in the two-dimensional array by a statement like StirFry[Count][0]. Count is the element of StirFry that's relevant, while 0 or 1 is the number of the needed element in the array that fills the Count space in StirFry.
You may find it helpful to think of Array[n][p]
references in terms of coordinates: The first number is the "down"
coordinate, while the second coordinate is the "across" coordinate.