Javascript array and dictionary Usage Analysis, javascript Array dictionary

Source: Internet
Author: User
Tags javascript array acer

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.

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.