JavaScript Multi-Dimensional Arrays?

Multi-dimensional arrays can be represented by arrays of arrays. For example, suppose that you want to create a 3×3 matrix, called Brady, and fill it with the strings shown in the following table:

 		col 0	col 1	col 2   row 0	"Marsha"	"Carol"	"Greg"   row 1	"Jan"	"Alice"	"Peter"   row 2	"Cindy"	"Mike"	"Bobby"

To create the array, you should first allocate storage for each element:

 Brady = new Array(3) for (i = 0; i < Brady.length; ++ i)	Brady [i] = new Array(3);

Although Brady is just a one-dimensional array, the fact that each of Brady's elements is initialized with yet another one-dimensional array means that Brady is an array of arrays--in other words, a two-dimensional array. Each element in the Brady array is a row in the matrix, and each row is an array of three elements.

Obviously, the expression Brady represents an array. In this case, however, so do the expressions Brady [0], Brady [1], and Brady [2]! This means that we can populate the Brady array with the following code:

 Brady [0] [0] = "Marsha";Brady [0] [1] = "Carol";Brady [0] [2] = "Greg";Brady [1] [0] = "Jan";Brady [1] [1] = "Alice";Brady [1] [2] = "Peter";Brady [2] [0] = "Cindy";Brady [2] [1] = "Mike";Brady [2] [2] = "Bobby";

Arrays would not be useful if their members could not be indexed with variables and expressions. The following JavaScript code will display arbitrary two-dimensional arrays of strings (such as the Brady array) in HTML tables. Please carefully observe the manner in which the row and col indices are used to iterate through each element of array. (If you are rusty on HTML table syntax, you might want to review it before wading through this function).

 function print_2d_string_array(array) { 	document.writeln ("") ;	var row; for (row = 0; row < ; 				array.length; ++row)	{ 		document.writeln (" ");			var col for (col = 0; col < array	 		[row].length; ++col) 			document.writeln (" "); 			document.writeln (" "); 	} 	document.writeln ("
" + array [row] [col] + "
");}print_2d_string_array (Brady);

If you really want a challenge, try making three-dimensional arrays using this same technique(an array of arrays of arrays)!

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job