On JavaScript array and dictionary _javascript skills

Source: Internet
Author: User
Tags javascript array acer

The array of Javascript arrays, both an array and a dictionary (Dictionary).

Let's take a look at the use of arrays.

Copy Code code as follows:

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

The code above creates an array with each element being a string object.

The array is then traversed. Note that the result of I is 0 and 1,a[i] is a string.

This is much like the properties of the traversal object mentioned in the previous article.

Next look at the use of the dictionary.

Copy Code code as follows:

var computer_price = new Array ();
computer_price["Acer"] = 500;
computer_price["Dell" = 600;
Alert (computer_price["Acer"]);

We can even walk through the array like the above (the dictionary)

Copy 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 results are:

Copy Code code as follows:

acer:500
dell:600

Next, take a look at the interesting things about JavaScript, or the example above.

We can think of Computer_price as a Dictionary object, and each of its key values is an attribute.

That is to say that Acer is a computer_price attribute. We can use it this way: Computer_price. Acer

Let's look at the simplified declarations of dictionaries and arrays.

Copy Code code as follows:

var array = [1, 2, 3]; Array
var array2 = {"Acer": $, "Dell": 600}; Dictionary
Alert (array2. Acer); 50

The statement to the dictionary is the same as the previous one. In our example, Acer is a key value, but also as a property of the Dictionary object.

The above is the entire content of this article, I hope you can enjoy, follow-up we will continue to update.

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.