Home  >  Article  >  Web Front-end  >  Introduction to various definition methods and common functions of arrays in JavaScript_Basic knowledge

Introduction to various definition methods and common functions of arrays in JavaScript_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 16:49:011382browse


Definition of array:
Method 1.

Copy code The code is as follows:

var mycars=new Array()
mycars[0]="sharejs.com"
mycars[1]="Volvo"
mycars[2]="BMW"

Method 2.
Definition and initialization together:

Copy code The code is as follows:
var mycars=new Array("Saab","Volvo","BMW");

Or:

Copy code The code is as follows:
var mycars=["Saab","Volvo ","BMW"];

Javascript two-dimensional array, use one-dimensional array to simulate:
Method 1.

Copy code The code is as follows:
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()
);

Javascript arrays do not need to set the length and will expand themselves. The array name.length returns the number of elements

Common functions for JavaScript arrays:
toString(): Convert the array into a string
toLocaleString(): Convert the array into a string
join(): Convert the array into a symbolically connected string
shift(): Remove an element from 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 tail of the array
concat(): Add to the array Element
slice(): Returns part of the array
reverse(): Sort the array in reverse order
sort(): Sort the array
splice(): Insert, delete or replace an array Element

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