您的位置:首页 > 其它

(AS3)操作数组的每个元素:forEach,every,filter,map,some

2012-07-18 17:11 489 查看
package
{
import flash.display.Sprite;

public class ArrayforEeach_etc extends Sprite
{
var book1:Object={name:"actionscript 3 殿堂之路",author:"kingda"};

var book2:Object={name:"flex 3 殿堂之路",author:"kingda"};
var book3:Object={name:"flash 3 殿堂之路",author:"kingda"};
var book4:Object={name:"大话设计模式",author:"ll"};
var bookList:Array=[book1,book2,book3,book4];

public function ArrayforEeach_etc()
{
trace("这套书没有过时的书?"+bookList.every(noflash));//every,返回是否每个对象都满足使noflash为true的条件
var newbookList:Array=bookList.map(mapNewList);//map,根据回调函数操作员数组的每个元素并利用回调函数返回的结果生成新的数组。
trace("新书单:"+newbookList[2].price);
trace("有kingda的书吗?"+bookList.some(isKingda));//只要有一个元素能让回调函数返回true,则some()返回true,否则false
var newbookList2:Array=bookList.filter(noKingda);//将符合回调函数条件的元素提取出来,构成一个新的数组并返回
trace("不是kingda的书:"+newbookList2[0].name);
bookList.forEach(showName);//为每个元素调用回调函数
}

function noflash(item:Object,index:int,arr:Array):Boolean
{
if(item.name.indexOf("flash")!=-1)
{
trace("第"+(index+1)+"本过时了");
return false;
}
return true;
}

function mapNewList(item:Object,index:int,arr:Array):Object
{
var newbook:Object=new Object();
newbook.name=item.name;
newbook.author=item.name;
newbook.price=1000;
return newbook;
}

function isKingda(item:Object,index:int,arr:Array):Boolean
{
if(item.author=="kingda")
return true;
return false;
}

function noKingda(item:Object,index:int,arr:Array):Boolean
{
if(item.author!="kingda")
return true;
return false;
}

function showName(item:Object,index:int,arr:Array):void
{
trace(item.name);
}
}
}


结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐