Javascript array and dictionary Usage Analysis, javascript Array dictionary
This document analyzes the usage of Javascript arrays and dictionaries. 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.
Copy codeThe 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.
Copy codeThe 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)
Copy codeThe 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.
Copy codeCode: 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.