Home  >  Article  >  Web Front-end  >  A brief description of javaScript Array (array) related methods_javascript skills

A brief description of javaScript Array (array) related methods_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:49:27773browse

1. There are two ways to create an Array object (when assigning an initial value):
var aColor=new Array('red','black','yellow');
alert(aColor.toString()); //output: red,black,yellow
var aColor=['red','black','blue'];
alert(aColor.toString());//output: red,black,blue
2.length: Get the length of the array
3.toString(): Output all elements in the array.
alert(aColor.toString());////output: red, black, yellow
4.valueOf(): Same as 3
5.join(): Connect the array values ​​and pass the array Connect with the connector and return the linked string
eg.alert(aColor.join("->"));//output:red->black->yellow
//(string)6.split (): Split the string into a character array, where split(" ") splits the string into a single character array, eg. "str" ​​is split into s, t, r
7.contact(): append the parameters At the end of the array, the returned function value is the new Array object
eg.
var aColor=new Array('red','black','yellow');
var aColorCon=aColor.concat( "1","2");
alert(aColorCon.toString());//output: red,black,yellow,1,2
8.slice(): Returns a new array with the specified items .
9.push()/pop(): push() adds an item to the end of the array, pop() deletes the last array item and returns the item value.
eg.
var aColor=new Array('red','black','yellow');
aColor.push("blue");
aColor.push("white") ;
alert(aColor.toString());
aColor.pop();
alert(aColor.toString());
10.shift(): Delete the first item in the array
11.unshift(): Add the first item to the array, and move the other items backward.
var aColor=new Array('red','black','yellow');
aColor.shift();
alert(aColor.toString());
aColor.unshift( "red");
alert(aColor.toString());
12.reverse(): Reverse the order of array items.
13.sort(): Sort array items in ascending order.
eg.
var aColor=new Array('red','black','yellow');
aColor.sort();
alert(aColor);
alert(aColor .reverse());
14.splice().

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