Array definition:
Method 1.
Copy codeThe Code is as follows:
Var mycars = new Array ()
Mycars [0] = "Saab"
Mycars [1] = "Volvo"
Mycars [2] = "BMW"
Method 2.
Definition and initialization together:
Var mycars = new Array ("Saab", "Volvo", "BMW ")
Or another method:
Var mycars = ["Saab", "Volvo", "BMW"];
Javascript two-dimensional array:
Javascript uses a one-dimensional array to simulate a two-dimensional array:
Method 1.
Var arr = new Array (['A', 'B', 'C'], ['D', 'E', 'F']);
Arr [0] returns the first one-dimensional array, and arr [0] [0] returns the first element 'a' of the first one-dimensional array, the same below.
Method 2.
Copy codeThe Code is as follows:
Arr = new Array ();
For (I = 0; I <100; I ++ ){
Arr [I] = new Array (...);
}
Method 3.
Copy codeThe Code is as follows:
Var arr = new Array (
New Array (),
New Array (),
New Array ()
);
Array length:
You do not need to set the length of the javascript array. The array name. length returns the number of elements.
Common functions:
Array common functions
ToString (): converts an array into a string.
ToLocaleString (): converts an array into a string.
Join (): converts an array into a string connected with symbols.
Shift (): removes an element from the array header.
Unshift (): insert an element in the header of the array.
Pop (): deletes an element from the end of the array.
Push (): Add an element to the end of the array.
Concat (): add elements to the array
Slice (): returns the part of the array
Reverse (): sorts arrays in reverse order.
Sort (): sorts arrays.
Splice (): insert, delete, or replace an array element.
Javascript array sorting:
Arrayobj. sort (sortfunction)
Parameters
ArrayObj
Array
SortFunction
Optional. Comparison functions. If this parameter is omitted, the elements are listed in ascending order of ASCII characters.
The comparison function must return one of the following values:
* Negative value. If the first parameter is smaller than the second parameter.
* Zero. If the two parameters are equal.
* Positive value. If the first parameter is greater than the second parameter
Example:
Var testArray = [1, 5, 2, 3, 6, 4]
TestArray. sort (function (a, B) {return a-B ;});
Alert (testArray );