The usual array is implicitly or displays the specified array subscript when it fills, but the array in JS can be assigned a value in the form of a name, which forms an associative array, such as:
var arr=New Array (); arr["China"]= "Beijing,niaoling,hulan"; arr["USA"]= " Newyork,washington,atlanta ", arr[" Japan "]=" Tokyo "; alert (arr[" China "]); Alert (arr[" Japan "]); Alert (arr[
Notice the above alert (arr[0]); This sentence, it will return undifined. This means that, in an associative array, an array element cannot be accessed with the traditional subscript, and must pass through the name of the element.
This form of access to array elements by name has the advantage of being highly readable and flexible and convenient. To a certain extent, it can be used as a hash table in JS.
When iterating over an associative array, you need to use a for-in loop, noting two different traversal methods in the following code:
var arr=New Array (); arr["China"]= "Beijing,niaoling,hulan"; arr["USA"]= " Newyork,washington,atlanta "; arr[" Japan "]=" Tokyo"// This way you can traverse the name of the associative array for (var in arr) {alert (item);} // this way you can iterate through the elements in the associative array for (var in arr) {alert (Arr[item]);}
Associative arrays in JavaScript