The definition of an array:
Method 1.
Copy Code code as follows:
var mycars=new Array ()
mycars[0]= "Saab"
mycars[1]= "Volvo"
mycars[2]= "BMW"
Method 2.
Define and initialize together:
var mycars=new Array ("Saab", "Volvo", "BMW")
Or another way of writing:
var mycars=["Saab", "Volvo", "BMW"];
JavaScript two-dimensional array:
JavaScript uses one-dimensional arrays to simulate two-dimensional arrays:
Method 1.
var arr = new Array ([' A ', ' B ', ' C '],[' d ', ' e ', ' f ']);
ARR[0] Returns the first one-dimensional array, arr[0][0] returns the first element ' a ' of the first one-dimensional array, the same below.
Method 2.
Copy Code code as follows:
Arr=new Array ();
for (i=0;i<100;i++) {
Arr[i]=new Array (...);
}
Method 3.
Copy Code code as follows:
var arr=new Array (
New Array (),
New Array (),
New Array ()
);
Array Length:
The JavaScript array does not need to be set in length, it expands itself, and the array name. Length returns the number of elements
Common functions:
Common functions for arrays
ToString (): Converts an array to a string
toLocaleString (): Converts an array to a string
Join (): Converts an array into a symbolic concatenated string
Shift (): Moves an element of an array's head out
Unshift (): Inserts an element in the header of an array
Pop (): Deletes an element from the tail of an array
Push (): Adds an element to the tail of the array
Concat (): Adding elements to an array
Slice (): Returns the part of an array
Reverse (): Sort the array in reverse order
Sort (): Sorting operations on array
Splice (): Inserts, deletes, or replaces an array element
JavaScript Array Ordering:
Arrayobj.sort (sortfunction)
Parameters
Arrayobj
Array
Sortfunction
Options available. comparison function. If this argument is omitted, then the elements are sorted in ascending order in ASCII characters.
The comparison function must return one of the following values:
* Negative value if the first argument passed is smaller than the second argument.
* Zero if two parameters are equal.
* Positive value if the first argument is larger than the second argument
Example:
var testarray=[1,5,2,3,6,4]
Testarray.sort (function (a,b) {return a-b;});
alert (Testarray);