1 package 2 { 3 import flash.display.Sprite; 4 5 public class ArrayforEeach_etc extends Sprite 6 { 7 var book1:Object={name:"actionscript 3 殿堂之路",author:"kingda"}; 8 9 var book2:Object={name:"flex 3 殿堂之路",author:"kingda"}; 10 var book3:Object={name:"flash 3 殿堂之路",author:"kingda"}; 11 var book4:Object={name:"大話設計模式",author:"ll"}; 12 var bookList:Array=[book1,book2,book3,book4]; 13 14 public function ArrayforEeach_etc() 15 { 16 trace("這套書沒有過時的書?"+bookList.every(noflash));//every,返回是否每個對象都滿足使noflash為true的條件 17 var newbookList:Array=bookList.map(mapNewList);//map,根據回調函數操作員數組的每個元素並利用回調函數返回的結果生成新的數組。 18 trace("新書單:"+newbookList[2].price); 19 trace("有kingda的書嗎?"+bookList.some(isKingda));//只要有一個元素能讓回調函數返回true,則some()返回true,否則false 20 var newbookList2:Array=bookList.filter(noKingda);//將符合回調函數條件的元素提取出來,構成一個新的數組並返回 21 trace("不是kingda的書:"+newbookList2[0].name); 22 bookList.forEach(showName);//為每個元素調用回調函數 23 } 24 25 function noflash(item:Object,index:int,arr:Array):Boolean 26 { 27 if(item.name.indexOf("flash")!=-1) 28 { 29 trace("第"+(index+1)+"本過時了"); 30 return false; 31 } 32 return true; 33 } 34 35 function mapNewList(item:Object,index:int,arr:Array):Object 36 { 37 var newbook:Object=new Object(); 38 newbook.name=item.name; 39 newbook.author=item.name; 40 newbook.price=1000; 41 return newbook; 42 } 43 44 function isKingda(item:Object,index:int,arr:Array):Boolean 45 { 46 if(item.author=="kingda") 47 return true; 48 return false; 49 } 50 51 function noKingda(item:Object,index:int,arr:Array):Boolean 52 { 53 if(item.author!="kingda") 54 return true; 55 return false; 56 } 57 58 59 function showName(item:Object,index:int,arr:Array):void 60 { 61 trace(item.name); 62 } 63 } 64 }
結果: