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

Javascript array and dictionary usage analysis_javascript skills

WBOY
Release: 2016-05-16 16:27:28
Original
1585 people have browsed it

This article analyzes the usage of Javascript arrays and dictionaries with examples. Share it with everyone for your reference. The specific analysis is as follows:

Javascript’s array Array is both an array and a dictionary.

Let’s take an example to see how to use arrays.

Copy code The code is 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, 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 very similar to traversing the properties of objects mentioned in the previous article.

Let’s take a look at how to use the dictionary.


Copy code 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 iterate over this array (dictionary) as above

Copy code 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. The output result is:
Acer: 500

Dell: 600

Next, let’s take a look at the interesting part of Javascript, still the above example.

We can think of computer_price as a dictionary object, and each key value is an attribute.

That is to say, Acer is an attribute of computer_price. We can use it like this: computer_price.Acer

Let’s take a look at the simplified declaration methods of dictionaries and arrays.


Copy code The code is as follows:
var array = [1, 2, 3]; // Array
var array2 = { "Acer": 500, "Dell": 600 }; // Dictionary
alert(array2.Acer); // 50
This declaration of the dictionary is the same as the previous one. In our example, Acer is a key value and can also be used as an attribute of the dictionary object.

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
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