【Array数组】使用slice返回数组中的元素

1、返回数组中的第一个元素

var letters:Array = new Array("a", "b", "c", "d", "e", "f");
var someLetters:Array = letters.slice(0,1);
trace(someLetters);//a

2、返回数组中最后一个元素

var letters:Array = new Array("a", "b", "c", "d", "e", "f");
var someLetters:Array = letters.slice(-1);
trace(someLetters);//f

3、返回数组中前两个元素

var letters:Array = new Array("a", "b", "c", "d", "e", "f");
var someLetters:Array = letters.slice(0,2);
trace(someLetters);//a,b

4、返回数组中倒数第二个元素

var letters:Array = new Array("a", "b", "c", "d", "e", "f");
var someLetters:Array = letters.slice(-2,-1);
trace(someLetters);//e

5、返回数组所有元素

var letters:Array = new Array("a", "b", "c", "d", "e", "f");
var someLetters:Array = letters.slice();
trace(someLetters);//a,b,c,d,e,f

 

6、总结,第一个参数是起始索引,如果第二个参数不写,就返回起始索引到数组最后所有的元素。参数不填,默认返回数组所有元素,也就是数组的浅复制。起始索引是负值是表示从后面开始。

 

posted @ 2012-11-14 17:44  ywl01  阅读(896)  评论(0编辑  收藏  举报