Home >Web Front-end >JS Tutorial >Use of javascript array_javascript skills

Use of javascript array_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:39:081220browse

Definition of array:

Method 1.

Copy code The 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 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.

Copy code The code is as follows:

arr=new Array() ;
for(i=0;i<100;i ) {
arr[i]=new Array(...);
}

Method 3.
Copy code The code is as follows:

var arr=new Array(
new Array() ,
new Array(),
new Array()
);

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);

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn