Associative array problem in JavaScript _ basics

Source: Internet
Author: User
Tags integer numbers

Copy Code code as follows:

var beatles = ["John", "Paul", "George", "Ringo"];

The Beatles array above is a typical example of a traditional array: the subscript for each element is a number, and the number increases by 1 for each additional element. The subscript for the first element is 0, and the subscript for the second element is 1. Analogy

If only the value of the element is given when the array is populated, the array will be a traditional array, and the subscript of its individual elements will be automatically created and refreshed.

You can change this default behavior by explicitly giving the subscript to each new element when the array is populated. When you give a subscript to a new element, you do not have to limit the use of integer numbers. You can also use a string:

Copy Code code as follows:

var Lennon = Array ();
lennon["Name" = "John";
lennon["Year"] = "1940";
lennon["Living"] = false;

Such an array is called an associative array. Because you can use strings instead of numeric values, your code is more readable. However, this usage is not a good habit and is not recommended for everyone to use. Essentially, when you create an associative array, you create the properties of the array object. In JavaScript, all variables are actually some kind of object. For example, a Boolean value is a Boolean object, and an array is a type of object array. In the example above, you actually add name to the Lennon array, and year and living are the attributes. Ideally, you should not modify the properties of an array object, but instead use a generic object.

The above is the entire content of this article, I hope you can enjoy.

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.