In C #, a multidimensional array can be defined in such a simple code:
Copy Code code as follows:
Int[,]myarray=new int[4,5];
How do you implement a multidimensional array without directly defining it in JavaScript?
First, define a one-dimensional array:
Copy Code code as follows:
var myarray=new Array ();
The members of a one-dimensional array are then defined as arrays (which can be redefined because JavaScript is a weak type):
Copy Code code as follows:
So far, you define a two-dimensional array with the first index of 0, and if you want to use the first two-dimensional array of 1, you still need to define it before you use it:
Copy Code code as follows:
<span style= "Font-family:calibri;" >myarray[1]=new Array ();</span>
Here is an example of a JavaScript multidimensional array application that uses multidimensional arrays to store multiple-choice questions and answers:
Copy Code code as follows:
<script type= "Text/javascript" >
To interpret the answer correctly
function Answercorrect (questionnumber, answer) {
var correct = false;
if (answer = = Answer[questionnumber])
correct = true;
return correct;
}
Defines an array of questions and answers to store topics and options
var questions = new Array ();
var answers = new Array ();
Define question 1, define the member with index 0 as a two-dimensional array
Questions[0] = new Array ();
Topics, defining two dimensional array members
Questions[0][0] = "The Beatles were:";
Answer
QUESTIONS[0][1] = "A sixties rock group from Liverpool";
QUESTIONS[0][2] = "Four musically gifted insected";
QUESTIONS[0][3] = "German Cars";
QUESTIONS[0][4] = "I don ' t know";
The answer to question 1
Answers[0] = "A"
Question 2
Definition Question 2
QUESTIONS[1] = new Array ();
Questions[1][0] = "Homer simpon ' s favorite food is:";
QUESTIONS[1][1] = "Fresd slead";
QUESTIONS[1][2] = "doughnuts";
QUESTIONS[1][3] = "sea food";
QUESTIONS[1][4] = "apples";
The answer to question 2
ANSWERS[1] = "B";
Prompt initialization complete
Alert ("Array initiallized");
</script>
PS: In the recent learning of JavaScript process, often use Notepad to write programs, and then changed to the. htm format, which is not as efficient as in VS or Dreamweaver, mainly without smart hints and highlighting. But you can remind yourself of every little detail, such as JavaScript sensitivity, how to write HTML tags and so on, hehe.