Multidimensional Arrays
Thursday, November 10, 2011 at 7:08AM The question of multidimensional arrays came up in class again yesterday and I wanted to reiterate that JavaScript does not have true multidimensional arrays. Let's (literally) take a page from the experts, here's what JavaScript: The Definitive Guide, 6th Ed, (O'Reilly Media, Inc., ISBN: 978-0-596-80552-4) has to say:
7.7. Multidimensional Arrays
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 variablematrixis an array of arrays of numbers. Every element inmatrix[x]is an array of numbers. To access a particular number within this array, you would writematrix[x][y]. Here is a concrete example that uses a two-dimensional array as a multiplication table:// Create a multidimensional array var table = new Array(10); // 10 rows of the table for(var i = 0; i < table.length; i++) table[i] = new Array(10); // Each row has 10 columns // Initialize the array for(var row = 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*7 var product = table[5][7]; // 35
Joe Paris | Comments Off |