Heim  >  Artikel  >  Web-Frontend  >  javaScript Array(数组)相关方法简述_javascript技巧

javaScript Array(数组)相关方法简述_javascript技巧

WBOY
WBOYOriginal
2016-05-16 18:49:27772Durchsuche

1.创建Array对象(赋初值情况下)两种方法:
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:获取数组长度
3.toString():输出数组中的所有元素。
alert(aColor.toString());////output: red,black,yellow
4.valueOf():同3
5.join():连接数组值,将数组通过连接符连接,返回链接后的字符串
eg.alert(aColor.join("->"));//output:red->black->yellow
//(string)6.split():将字符串分割为字符数组,其中split(" ")将字符串分割为单个字符数组,eg."str"分割为s,t,r
7.contact():将参数附加到数组末尾,返回的函数值是新的Array对象
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():返回具有指定项的新数组。
9.push()/pop():push()在数组结尾添加一个项,pop()删除最后一个数组项并将该项值返回。
eg.
var aColor=new Array('red','black','yellow');
aColor.push("blue");
aColor.push("white");
alert(aColor.toString());
aColor.pop();
alert(aColor.toString());
10.shift():删除数组中第一个项
11.unshift():向数组添加第一项,其他的各项后移。
var aColor=new Array('red','black','yellow');
aColor.shift();
alert(aColor.toString());
aColor.unshift("red");
alert(aColor.toString());
12.reverse():颠倒数组项的顺序。
13.sort():对数组项升序排序。
eg.
var aColor=new Array('red','black','yellow');
aColor.sort();
alert(aColor);
alert(aColor.reverse());
14.splice().

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn