AS3数组学习

indexOf lastIndexOf

返回索引,查找不到返回-1

var arr:Array = ["a","b","c","d","e","f","g"]; 
arr.indexOf("e"); 
arr.indexOf("b",2);//从第3个元素开始查找 
arr.lastIndexOf("b",2);//从第3个元素反向查找

push unshift

在首尾插入元素,返回增加元素后数组的长度

var arr:Array = ["a","b","c","d","e","f","g"]; 
arr.push("h");//在数组尾部插入元素h 
arr.push("i","j","k");//在数组尾部插入多个元素 
arr.unshift("h");//在数组头部插入元素h 
arr.unshift("i","j","k");//在数组头部插入多个元素

pop shift

删除首尾元素,返回被删除的元素

var arr:Array = ["a","b","c","d","e","f","g"]; 
arr.pop();//删除尾元素 
arr.shift();//删除首元素 

splice

删除数组一段连续的元素,返回被删除的元素数组

var arr:Array = ["a","b","c","d","e","f","g"]; 
arr.splice(删除点的索引,要删除的个数,新元素1,新元素2); 
arr.splice(2,3,"o","p","q");//从索引为2开始,删除3个元素,并插入3个新元素"o","p","q"
arr.splice(-2,3,"o","p","q");//从倒数为2的索引开始,删除3个元素,并插入3个新元素"o","p","q"
arr.splice(2,3);//单纯的删除从索引2开始的3个元素(不插入) 
arr.splice(2);//单纯的删除从索引2开始的所有元素(不插入) 
arr.splice(2,0,"x","y","z");//在索引2的后面插入3个新元素(不删除)

slice

获取数组中的部分元素

var arr:Array = ["a","b","c","d","e","f","g"]; 
var newArr:Array = arr.slice();//浅复制数组 
var newArr:Array = arr.slice(2); 
var newArr:Array = arr.slice(-2); 
var newArr:Array = arr.slice(2,5); 

concat

将多个元素或数组拼接成一个新数组

var arr:Array = ["a","b","c","d","e","f","g"]; 
var newArr = arr.concat("x","y","z");//原数组不变,新数组是原数组拼接后的结果 
var newArr = arr.concat();//浅复制数组

reverse

将数组倒序排列

var arr:Array = ["a","b","c"]; 
arr.reverse();//得到"c","b","a" 

sort

sort() 按默认排序,注意:区分大小写,数字也是按字符串来处理

var arr:Array = ["b","c","a"]; 
trace(arr.sort());//得到a,b,c

arr = ["210","1111","30"]; 
trace(arr.sort());//得到1111,210,30

arr = ["b","X","A","c"]; 
trace(arr.sort());//得到A,X,b,c

arr = ["b","C",235,"a",38,"X",8641]; 
trace(arr.sort());//得到235,38,8641,C,X,a,b

 

sort(args) 按参数排序,一共有5种(注意:除了Array.RETURNINDEXEDARRAY,其它方式只要排序成功,都会实际改变数组)

1. sort(Array.CASEINSENSITIVE) 同sort(),不过不区分大小写,数字也是按字符串来处理

var arr = ["b","X","A","c"]; 
arr.sort(Array.CASEINSENSITIVE);//得到"A","b","c","X"

2. sort(Array.DESCENDING) 按字符串的倒序排列,区分大小写,数字也是按字符串来处理

var arr:Array = ["b","C","a","X",235,8641,38]; 
trace(arr.sort(Array.DESCENDING));//得到b,a,X,C,8641,38,235

3. sort(Array.UNIQUESORT) 同sort(),如果数组有相同元素,则放弃排序,返回0

var arr:Array = ["b","C","a","X",235,8641,38,2365]; 
if(arr.sort(Array.UNIQUESORT)=="0") 
{  
    trace("有相同元素"); 
} 
else 
{  
    trace("没有相同元素"); 
}

4. sort(Array.RETURNINDEXEDARRAY) 同sort(),返回排序后的索引列表,原数组不变

var arr:Array = ["b","C","a","X",235,8641,38,2365]; 
trace(arr.sort(Array.RETURNINDEXEDARRAY));//得到4,7,6,5,1,3,2,0 
trace(arr);//得到b,C,a,X,235,8641,38,2365(原数组)

5. sort(Array.NUMERIC) 按数字从小到大排序,如果数组里含有非数字,会抛出异常

var arr:Array = [235,8641,38,2365]; 
trace(arr.sort(Array.NUMERIC));//得到38,235,2365,8641

sort(函数名) 按自定义函数比较,该函数必须接受2个参数,返回值为1、-1、0

public function Sample1008() 
{ 
    var objA:Object = {price:200,quantity:2}; 
    var objB:Object = {price:400,quantity:4}; 
    var objC:Object = {price:100,quantity:1}; 
    var arr:Array = [objA,objB,objC]; 
    trace("原数组是:"); 
    for(var i:int=0;i<arr.length;i++) 
    { 
        trace(arr[i].price); 
    } 
    arr.sort(Compare); 
    trace("排序后的数组是:"); 
    for(var j:int=0;j<arr.length;j++) 
    { 
        trace(arr[j].price); 
    } 
} 
private function Compare(paraA:Object,paraB:Object):int 
{ 
    var totalPriceA:int = paraA.price * paraA.quantity; 
    var totalPriceB:int = paraB.price * paraB.quantity; 
    if(totalPriceA>totalPriceB) 
    { 
        return 1; 
    } 
    else if(totalPriceA<totalPriceB) 
    { 
        return -1; 
    } 
    else 
    { 
        return 0; 
    } 
} 
/*运行结果是 
原数组是: 
200 
400 
100 
排序后的数组是: 
100 
200 
400*/

sortOn

常用于对象数组的排序

var obj1:MyClass = new MyClass(); 
var obj2:MyClass = new MyClass(); 
var obj3:MyClass = new MyClass(); 
obj1.orderID = 300; 
obj2.orderID = 100; 
obj3.orderID = 200; 
var arr:Array = [obj1,obj2,obj3]; 
//trace(arr.sortOn("orderID")); 
trace(arr.sortOn("orderID",Array.RETURNINDEXEDARRAY));//得到索引1,2,0

every

判断数组里的每个元素是否满足某些条件,只有全部都满足才为true

public function Sample1008() 
{ 
    //必须5个人都满70级,才可以进副本
   var player1:Object = {name:"wayne",level:70}; 
    var player2:Object = {name:"tom",level:69}; 
    var player3:Object = {name:"jacky",level:70}; 
    var player4:Object = {name:"andy",level:68}; 
    var player5:Object = {name:"bill",level:70}; 
     
    var playerList:Array = [player1,player2,player3,player4,player5]; 
    if(!playerList.every(CheckLevel)) 
    { 
        trace("队伍中有人等级未到70,无法进入副本"); 
    } 
} 
private function CheckLevel(item:*,index:int,arr:Array):Boolean 
{ 
    if(item.level<70) 
    { 
       //trace(item.name+"等级未到70,无法进入副本"); 
        return false; 
    } 
    return true; 
}

map

映射器,逐个处理数组中的元素,最后生成一个修改后的新数组(不会更改原数组)

public function Sample1008() 
{ 
    var player1:Object = {name:"wayne",level:70}; 
    var player2:Object = {name:"tom",level:69}; 
    var player3:Object = {name:"jacky",level:70}; 
    var player4:Object = {name:"andy",level:68}; 
    var player5:Object = {name:"bill",level:70}; 
     
    var playerList:Array = [player1,player2,player3,player4,player5]; 
     
    for(var i:int = 0; i<playerList.length; i++) 
    { 
        trace("player"+(i+1)+"的所有属性是:"); 
        for(var attr:String  in  playerList[i]) 
        { 
            trace(attr+":"+playerList[i][attr]); 
        } 
    } 
     
   //为每个玩家添加新属性"声望值"
    var newArray:Array = playerList.map(AddNewAttribute); 
     
    trace("-----------------Map后的结果-----------------"); 
     
    for(var j:int = 0; j<newArray.length; j++) 
    { 
        trace("player"+(j+1)+"的所有属性是:"); 
        for(var attr2:String  in  newArray[j]) 
        { 
            trace(attr2+":"+newArray[j][attr2]); 
        } 
    } 
} 
private function AddNewAttribute(item:*,index:int,arr:Array):Object 
{ 
    var tmpPlayer:Object = {}; 
    tmpPlayer.name = item.name; 
    tmpPlayer.level = item.level; 
    tmpPlayer.repute = 99; 
    return tmpPlayer; 
}

some

判断数组里的每个元素是否满足某些条件,只要有一个满足就返回true

public function Sample1008() 
{ 
   //只要队伍里有一个人声望达到140以上,则全队可以进副本 
   var player1:Object = {name:"wayne",level:70,repute:100}; 
    var player2:Object = {name:"tom",level:70,repute:190}; 
    var player3:Object = {name:"jacky",level:70,repute:50}; 
    var player4:Object = {name:"andy",level:70,repute:230}; 
    var player5:Object = {name:"bill",level:70,repute:120}; 
     
    var playerList:Array = [player1,player2,player3,player4,player5]; 
     
    if(playerList.some(CheckRepute)) 
    { 
        trace("可以进入副本"); 
    } 
    else 
    { 
        trace("队伍里没有成员声望达到140,无法进入副本"); 
    } 
} 
private function CheckRepute(item:*,index:int,arr:Array):Boolean 
{ 
    if(item.repute>=140) 
    { 
        return true; 
    } 
    return false; 
}

filter

将数组里满足特定条件的元素提取出来,生成一个新数组

public function Sample1008() 
{ 
   //得到队伍里所有声望达到要求的成员 
   var player1:Object = {name:"wayne",level:70,repute:100}; 
    var player2:Object = {name:"tom",level:70,repute:190}; 
    var player3:Object = {name:"jacky",level:70,repute:50}; 
    var player4:Object = {name:"andy",level:70,repute:230}; 
    var player5:Object = {name:"bill",level:70,repute:120}; 
     
    var playerList:Array = [player1,player2,player3,player4,player5]; 
     
    var newArray:Array = playerList.filter(CheckRepute); 
     
    for(var i:int=0; i<newArray.length; i++) 
    { 
        for(var attr:String  in  newArray[i]) 
        { 
            trace(attr+":"+newArray[i][attr]); 
        } 
    } 
} 
private function CheckRepute(item:*,index:int,arr:Array):Boolean 
{ 
    if(item.repute>=140) 
    { 
        return true; 
    } 
    return false; 
}

forEach

对数组里的元素逐个进行操作,实际改变了数组(不同于map)

public function Sample1008() 
{ 
   //在每个玩家名字前面加上公会名称 
   var player1:Object = {name:"wayne",level:70,repute:100}; 
    var player2:Object = {name:"tom",level:70,repute:190}; 
    var player3:Object = {name:"jacky",level:70,repute:50}; 
    var player4:Object = {name:"andy",level:70,repute:230}; 
    var player5:Object = {name:"bill",level:70,repute:120}; 
     
    var playerList:Array = [player1,player2,player3,player4,player5]; 
     
    playerList.forEach(BuildName); 
     
    for(var i:int=0; i<playerList.length; i++) 
    { 
        for(var attr:String  in  playerList[i]) 
        { 
            trace(attr+":"+playerList[i][attr]); 
        } 
    } 
} 
private function BuildName(item:*,index:int,arr:Array):void 
{ 
    item.name = "暗界--"+item.name; 
}

 

posted @ 2010-07-15 14:59  CoderWayne  阅读(10022)  评论(1编辑  收藏  举报