One, array arrays
Arrays in JavaScript are also one or more collections that have the same data type. Usage is similar to arrays in Java.
Common properties and methods for array objects:
Property:
Length: Gets the size of the array;
Method:
Join (): puts all the elements in the array into a string separated by delimiters.
Sort (): Sorts the array elements.
1. Creating an array
Grammar:
var a1 = new Array (); Creates an array with a length of 0;
var a2 = new Array (size); Creates an array of the specified length, an array with length of size, and an array of undefined elements
var a3 = new Array (element0, Element1, ..., ELEMENTN); Creates an array based on a predefined value. The length is the number of elements.
2. Assigning values to arrays
(1) First declaration and re-assignment
var pro = new Array (4);
Pro[0] = "Guangdong province";
PRO[1] = "Hubei province";
PRO[5] = "Guangxi province"; The original length is 4, and when you assign a value to an element with subscript 5, the array length becomes 6.
Elements are accessed through the subscript, and elements are traversed through a for loop
(2) Use the character designator to access the element:
pro["GD"] = "Guangdong province";
If you use a string representation you can only use the for: In ... The method is convenient and the For method cannot access the data.
(3) Simultaneous initialization of declarations:
var pro = new Array ("Beijing", "Shanghai", "Tianjin", "Chongqing city", "Guangdong province", "Hubei province");
(4) directly initialized to a two-dimensional array:
var citylist = new Array ();
citylist["Guangdong province"] = ["Guangzhou", "Shenzhen", "Dongguan"];
citylist["Hubei province"] = ["Wuhan City", "Xiangyang City"];
3, the operation of the array:
(1) The array reads:
Array [subscript] or array ["key"] to read
(2) Array traversal:
Traversal through for loop: An array using subscript method
for (Var i=0;i<array.length;i++) {
Alert (Pro[i]);
}
Through for: In.. Traversal: Array using the keyword method
for (var i in array) {
Alert (Pro[i]);
}
A two-dimensional array requires a nested loop to read
for (var i in citylist) {
for (Var j in Citylist[i]) {
Alert (Citylist[i][j]);
}
}
Java Script Basics (eight) array arrays object