JavaScript Arrays and dictionary usage analysis

Source: Internet
Author: User
Tags javascript array acer

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.