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.