Home > Web Front-end > JS Tutorial > body text

How to implement multi-dimensional arrays in JavaScript_javascript skills

WBOY
Release: 2016-05-16 17:13:37
Original
1524 people have browsed it

In C#, defining a multi-dimensional array can be achieved with such a simple code:

Copy the code The code is as follows:

int[,]myArray=new int[4,5];

Multi-dimensional arrays cannot be implemented through direct definition in JavaScript, so how to implement it?
First define a one-dimensional array:
Copy the code The code is as follows:

var myArray= new Array();

Then define the members of the one-dimensional array as an array (redefinition is possible because JavaScript is weakly typed):
Copy code The code is as follows:

myArray[0]=new Array();

That’s it , a two-dimensional array with the first index 0 is defined. If you want to use the two-dimensional array with the first index 1, you still need to define it before use:
Copy code The code is as follows:

myArray[1]=new Array() ;

The following is an example of JavaScript multi-dimensional array application, which uses multi-dimensional arrays to store the questions and answers of multiple-choice questions:

Copy code The code is as follows:



PS: In the process of learning JavaScript recently, I often use Notepad to write programs, and then change them to .htm format to run. This is not as efficient as in VS or DreamWeaver, mainly because there are no smart prompts and highlights. But you can remind yourself to pay attention to every little detail, such as JavaScript case sensitivity, how to write Html tags, etc., haha.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template