Home > Web Front-end > JS Tutorial > Associative array issues in JavaScript_Basic knowledge

Associative array issues in JavaScript_Basic knowledge

WBOY
Release: 2016-05-16 16:11:15
Original
1018 people have browsed it

Copy code The code is as follows:

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

The beatles array above is a typical example of a traditional array: the subscript of each element is a number, and each time an element is added, the number increases by 1. The index of the first element is 0, and the index of the second element is 1. And so on.

If only the values ​​of the elements are given when filling the array, the array will be a traditional array, and the subscripts of its individual elements will be automatically created and refreshed.

This default behavior can be changed by explicitly giving the subscript for each new element when filling the array. When giving subscripts for new elements, you are not limited to using integer numbers. You can also use strings:

Copy code The code is 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, the code is more readable. However, this usage is not a good habit and is not recommended for everyone. Essentially, when you create an associative array, you create properties of the Array object. In JavaScript, all variables are actually objects of some type. For example, a Boolean value is an object of type Boolean, and an array is an object of type Array. In the above example, you actually added the name, year and living attributes to the lennon array. 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 all like it.

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