Multidimensional Arrays in JavaScriptQuestion: How do I create a two-dimensional array in JavaScript? Answer: JavaScript does not have a special syntax for creating multidimensional arrays. A common workaround is to create an array of arrays in nested loops. (This technique is used, for example, to define the game board arrays in several games on this site, such as the JavaScript Tetris game.)
The following code example illustrates the array-of-arrays technique. First, this code creates an array var iMax = 20; var jMax = 10; var f = new Array(); for (i=0;i<iMax;i++) { f[i]=new Array(); for (j=0;j<jMax;j++) { f[i][j]=0; } }
See also:
|
Copyright © 1999-2011, JavaScripter.net.