<?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({data: 1, label: "one"}); myArray.push({data: 1, label: "one"}); myArray.push({data: 1, label: "one"}); myArray.push({data: 1, label: "one"}); myArray.push({data: 2, label: "two"}); myArray.push({data: 2, label: "two"}); myArray.push({data: 2, label: "two"}); myArray.push({data: 1, label: "one"}); myArray.push({data: 3, label: "three"}); myArray.push({data: 3, label: "three"}); yuan.dataProvider=myArray; // 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.hasOwnProperty(item.data)) { /* If the keys Object already has this property, return false and discard this item. */ return false; } else { /* Else the keys Object does *NOT* already have this key, so add this item to the new data provider. */ keys[item.data]=item; return true; } // if (keys.indexOf(arr[idx]) != -1) // { // trace(arr[idx]); // trace(1); // return false; // } // else // { // trace(arr[idx]); // trace(-1); // 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>