Http://www.68idc.cn/help/makewebs/javascript/20141214142024.html
This article mainly introduces the JavaScript array and dictionary usage, in the case of the form of a more detailed analysis of the array as the different use of the dictionary, with a certain reference value, the need for friends can refer to the following
In this paper, we analyze JavaScript arrays and dictionary usages. Share to everyone for your reference. The specific analysis is as follows:
An array of Javascript arrays, both an array and a dictionary (Dictionary).
Let's look at the use of arrays first.
Copy the code code as follows:
var a = new Array ();
A[0] = "Acer";
A[1] = "Dell";
for (var i in a) {
alert (i);
}
The above code creates an array, and each element is a string object.
The array is then traversed. Note that the result of I is 0 and 1,a[i] is only a string.
This is much like the property of traversing an object as mentioned in the previous article.
Let's look at the usage of the dictionary below.
Copy the code code as follows:
var computer_price = new Array ();
computer_price["Acer"] = 500;
computer_price["Dell"] = 600;
Alert (computer_price["Acer");
We can even traverse the array (dictionary) as well as above
Copy the code code as follows:
for (var i in Computer_price) {
Alert (i + ":" + computer_price[i]);
}
Here I is the dictionary of each key value. The output is:
acer:500
dell:600
Below, take a look at the interesting point of JavaScript, or the example above.
We can think of Computer_price as a Dictionary object, and each of its key values is an attribute.
This means that Acer is a property of Computer_price. We can use it this way: Computer_price. Acer
Let's take a look at the simplified declaration method for dictionaries and arrays.
Copy the code code as follows:
var array = [1, 2, 3]; Array
var array2 = {"Acer": $, "Dell": 600}; Dictionary
Alert (array2. Acer); 50
So the declaration of the dictionary is the same as the previous one. In our case, Acer is the key value, but also as the property of the Dictionary object.
Hopefully this article will help you with JavaScript programming.
JavaScript Arrays and dictionary usage analysis