Home >Web Front-end >JS Tutorial >Use of javascript array_javascript skills
Definition of array:
Method 1.
Method 2.
Definition and initialization together:
var mycars=new Array("Saab","Volvo","BMW")
or another way of writing:
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, arr[0][0] returns the first element 'a' of the first one-dimensional array, the same below.
Method 2.
Array length:
Javascript arrays do not need to set the length, they will expand themselves. The array name.length returns the number of elements
Commonly used functions:
Common array functions
toString(): Convert the array into a string
toLocaleString(): Convert the array into a string
join(): Convert the array into a symbolically connected string
shift (): Move an element out of the head of the array
unshift(): Insert an element at the head of the array
pop(): Delete an element from the tail of the array
push(): Add an element To the end of the array
concat(): Add elements to the array
slice(): Return part of the array
reverse(): Reverse sort the array
sort(): Sort the array
splice(): Insert, delete or replace an array element
javascript array sorting:
arrayobj.sort(sortfunction)
Parameters
arrayObj
Array
sortFunction
Optional. Comparison function. If this parameter is omitted, the elements will be sorted in ascending ASCII character order.
The comparison function must return one of the following values:
* Negative value, if the first parameter passed is smaller than the second parameter.
* Zero if both arguments are equal.
* Positive value, if the first parameter is larger than the second parameter
Example:
var testArray=[1,5,2,3,6,4]
testArray.sort(function(a,b){return a-b;});
alert(testArray);