在FLEX中过滤简单数组中的重复元素.
在flexexamples看到一篇过滤数组重复元素的文章,
不过是复杂数组,
自己做了一个过滤最简单数组的方法,与大家分享:
代码 :
不过是复杂数组,
自己做了一个过滤最简单数组的方法,与大家分享:
代码 :
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()" fontSize="12">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
private var myArray:Array=new Array();
private var filteredArr:Array=new Array();
private var keys:Array =new Array();
private function init():void
{
myArray.push("wang");
myArray.push("wang");
myArray.push("yan");
myArray.push("wang");
myArray.push("xiaoxiaocainiao");
myArray.push("xiaoxiaocainiao");
myArray.push("xiaoxiaocainiao");
myArray.push("小小菜鸟");
myArray.push("小小菜鸟");
myArray.push("小小菜鸟");
yuan.dataProvider=myArray;
}
private function onclick(event:MouseEvent):void
{
keys=null;
keys= new Array();
filteredArr=myArray.filter(removedDuplicates);
mubiao.dataProvider=filteredArr;
}
private function removedDuplicates(item:*, idx:uint, arr:Array):Boolean {
if (keys.indexOf(arr[idx])!=-1) {
return false;
} else {
keys.push(arr[idx]);
return true;
}
}
//过滤数组中重复元素--结束
]]>
</mx:Script>
<mx:Button x="144" y="285" label="过滤" click="onclick(event)" fontSize="12"/>
<mx:List id="yuan" x="10" y="47" width="150" height="200"></mx:List>
<mx:List id="mubiao" x="177" y="47" width="150" height="200"></mx:List>
<mx:Label x="51" y="21" text="源数组" fontSize="12"/>
<mx:Label x="189" y="21" text="目标数组" fontSize="12"/>
</mx:Application>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()" fontSize="12">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
private var myArray:Array=new Array();
private var filteredArr:Array=new Array();
private var keys:Array =new Array();
private function init():void
{
myArray.push("wang");
myArray.push("wang");
myArray.push("yan");
myArray.push("wang");
myArray.push("xiaoxiaocainiao");
myArray.push("xiaoxiaocainiao");
myArray.push("xiaoxiaocainiao");
myArray.push("小小菜鸟");
myArray.push("小小菜鸟");
myArray.push("小小菜鸟");
yuan.dataProvider=myArray;
}
private function onclick(event:MouseEvent):void
{
keys=null;
keys= new Array();
filteredArr=myArray.filter(removedDuplicates);
mubiao.dataProvider=filteredArr;
}
private function removedDuplicates(item:*, idx:uint, arr:Array):Boolean {
if (keys.indexOf(arr[idx])!=-1) {
return false;
} else {
keys.push(arr[idx]);
return true;
}
}
//过滤数组中重复元素--结束
]]>
</mx:Script>
<mx:Button x="144" y="285" label="过滤" click="onclick(event)" fontSize="12"/>
<mx:List id="yuan" x="10" y="47" width="150" height="200"></mx:List>
<mx:List id="mubiao" x="177" y="47" width="150" height="200"></mx:List>
<mx:Label x="51" y="21" text="源数组" fontSize="12"/>
<mx:Label x="189" y="21" text="目标数组" fontSize="12"/>
</mx:Application>