This article mainly introduces the use of Javascript arrays and dictionaries. It analyzes in detail the usage of arrays as arrays and dictionaries in the form of examples, which has some reference value, for more information about how to use Javascript arrays and dictionaries, see the examples in this article. Share it with you for your reference. The specific analysis is as follows:
The Array of Javascript is both an Array and a Dictionary ).
Let's take an example to see how to use arrays.
The Code is as follows:
Var a = new Array ();
A [0] = "Acer ";
A [1] = "Dell ";
For (var I in ){
Alert (I );
}
The code above creates an array, and each element is a string object.
Then traverse the array. Note that the result of I is 0 and 1, and the result of a [I] is a string.
This is similar to the property of traversing objects mentioned in the previous article.
Next, let's look at the usage of the dictionary.
The Code is as follows:
Var computer_price = new Array ();
Computer_price ["Acer"] = 500;
Computer_price ["Dell"] = 600;
Alert (computer_price ["Acer"]);
We can even traverse this array (dictionary) as above)
The Code is as follows:
For (var I in computer_price ){
Alert (I + ":" + computer_price [I]);
}
Here I is each key value of the dictionary. Output result:
Acer: 500
Dell: 1, 600
Next, let's take a look at the interesting part of Javascript, or the example above.
We can regard computer_price as a dictionary object, and each key value of computer_price is an attribute.
That is to say, Acer is an attribute of computer_price. We can use it like this: computer_price.Acer
Next, let's take a look at the simplified declaration of dictionaries and arrays.
The Code is as follows:
Var array = [1, 2, 3]; // array
Var array2 = {"Acer": 500, "Dell": 600}; // dictionary
Alert (array2.Acer); // 50
In this way, the dictionary statement is the same as the previous one. In our example, The Acer is a key value, but also an attribute of the dictionary object.
I hope this article will help you design javascript programs.