JavaScript Multi-Dimensional Arrays?

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:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as