Two Dimensional Arrays

Visualizing Two Dimensional Arrays

Whereas, one dimensional arrays can be visualized as a stack of elements, two dimensional arrays can be visualized as a multicolumn table or grid.

For example, we could create a two dimensional array that holds three columns of data; a question, an answer, and a topic.

Two Dimensional Array
0 Arrays What is an array? An ordered stack of data
1 Arrays How to create arrays? Assign variable name to array object, then assign values to the array.
2 Arrays What are two dimensional arrays? An ordered grid of data

Creating Two Dimensional Arrays

Generally, creating two dimensional arrays is very similar to creating one dimensional arrays. Some languages allow you to create two dimensional arrays simply by adding an index item, however JavaScript doesn't support two dimensional arrays.

JavaScript, does however, allow you to simulate a two dimensional array. You can do this by creating an "array of an array".

To do this, you create an array, loop through the array, and for each element, you create another array. Then, you simply add an index for each column of your grid. In JavaSript this would look something like this:

Copy to Clipboard
var faq = new Array(3)

for (i=0; i <3; i++)
faq[i]=new Array(3)

faq[0][1] = "Arrays"
faq[0][2] = "What is an array?"
faq[0][3] = "An ordered stack of data"

faq[1][1] = "Arrays"
faq[1][2] = "How to create arrays?"
faq[1][3] = "Assign variable name to array object, then assign values to the array."

faq[2][1] = "Arrays"
faq[2][2] = "What are two dimensional arrays?"
faq[2][3] = "An ordered grid of data"

Enjoy this page?