Home > Web Front-end > JS Tutorial > A brief discussion on Javascript arrays and dictionaries

A brief discussion on Javascript arrays and dictionaries

PHPz
Release: 2018-09-29 16:03:05
Original
1286 people have browsed it

This article mainly briefly introduces Javascript arrays and dictionaries. Friends in need can refer to it.

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

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

var a = new Array();  
a[0] = "Acer";  
a[1] = "Dell";  
for (var i in a) {  
    alert(i);  
}
Copy after login

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 the properties of traversing objects mentioned in the previous article.

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

var computer_price = new Array();  
computer_price["Acer"] = 500;  
computer_price["Dell"] = 600;  
alert(computer_price["Acer"]);
Copy after login

We can even traverse this array (dictionary) as above

for (var i in computer_price) {  
    alert(i + ": " + computer_price[i]);  
}
Copy after login

where i is each key value of the dictionary. The output result is:

Acer: 500
Dell: 600
Copy after login

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 method of dictionaries and arrays.

var array = [1, 2, 3]; // 数组  
var array2 = { "Acer": 500, "Dell": 600 }; // 字典  
alert(array2.Acer); // 50
Copy after login

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.

The above is the entire content of this article. I hope you all like it. For more related tutorials, please visit JavaScript Video Tutorial!

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