May 2011
Intermediate to advanced
1096 pages
40h 54m
English
JavaScript does not support true multidimensional arrays, but
you can approximate them with arrays of arrays. To access a value in
an array of arrays, simply use the [] operator twice. For example, suppose the
variable matrix is an array of
arrays of numbers. Every element in matrix[x] is an array of numbers. To access
a particular number within this array, you would write matrix[x][y]. Here is a concrete example
that uses a two-dimensional array as a multiplication table:
// Create a multidimensional arrayvartable=newArray(10);// 10 rows of the tablefor(vari=0;i<table.length;i++)table[i]=newArray(10);// Each row has 10 columns// Initialize the arrayfor(varrow=0;row<table.length;row++){for(col=0;col<table[row].length;col++){table[row][col]=row*col;}}// Use the multidimensional array to compute 5*7varproduct=table[5][7];// 35
Read now
Unlock full access