This article mainly introduces related information about the Association array in JavaScript. For more information, see
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. Each time an element is added, the number is increased by 1 in turn. The subscript of the first element is 0, and that of the second element is 1. And so on.
If only the element value is given when the array is filled, the array will be a traditional array, and the subscript of each element will be automatically created and refreshed.
This default behavior can be changed by explicitly providing subscript for each new element when filling the array. You do not have to limit the number to an integer when giving a marker for a new element. You can also use a string:
The Code is as follows:
Var lennon = Array ();
Lennon ["name"] = "John ";
Lennon ["year"] = "1940 ";
Lennon ["living"] = false;
Such an array is called an associated array. Because strings can be used to replace numeric values, the code is more readable. However, this usage is not a good habit and is not recommended. Essentially, when creating an associated Array, you create the attributes of an Array object. In JavaScript, all variables are actually some type of objects. For example, a Boolean value is a Boolean object, and an Array is an Array object. In the above example, you actually added name to the lennon array, and year and living are the attributes. Ideally, you should not modify the attributes of an Array Object, but use a common Object ).
The above is all the content of this article. I hope you will like it.