//INIT: Creating two dimensional arrays

<New code>

//INIT: — define a two dimensional array

var exampleArr	=[	// rows 0-3, columns 0-3
	[0,1,0,0], // row 0 = exampleArr [ 0 ]
	[0,1,1,0], // row 1 = exampleArr [ 1 ]
	[0,0,1,0], // row 2 = exampleArr [ 2 ]
	[0,0,1,0]  // row 3 = exampleArr [ 3 ]
];

Two dimensional arrays

You are quite familiar with one-dimensional arrays by now. You so you know that...


var colourArray =[
	"Red", 			// element 0
	"Orange", 		// element 1
	"Yellow", 		// element 2
	"Green", 		// element 3
	"Blue", 		// element 4
	"Indigo", 		// element 5
	"Violet"		// element 6
];

...will create colorArray with the following structure:

Index 0 1 2 3 4 5 6
Data Red Orange Yellow Green Blue Indigo Violet

Introducing two-dimensional arrays

Now imagine an array where each element is an array, like this:


var sampleArray =[	
	[ "Cyan", 	"Yellow", 	"Magenta"	],		// row 0
	[ 1, 		2, 		3		],		// row 1
	[ "a", 		"b", 		"c"		],		// row 2
	[ "gold", 	"silver", 	"bronze"		]		// row 3
];

This will create a two dimensional array, with data stored like this:

[x][0] [x][1] [x][2]
[0][x] sampleArray[0][0] = sampleArray[0][1] = sampleArray[0][2] =
[1][x] sampleArray[1][0] = sampleArray[1][1] = sampleArray[1][2] =
[2][x] sampleArray[2][0] = sampleArray[2][1] = sampleArray[2][2] =
[3][x] sampleArray[3][0] = sampleArray[3][1] = sampleArray[3][2] =

Take a guess which values will contained in each cell, and then click to see if you are right.

Learn more