flex 去除Array中的重复数据 过滤重复值

来源:互联网 发布:娥佩兰旗舰店真假 知乎 编辑:程序博客网 时间:2024/05/16 06:35

<?xml version="1.0" encoding="utf-8"?>   
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"   
                layout="vertical"   
                verticalAlign="middle"   
                backgroundColor="white"   
                creationComplete="init()">   
      
    <mx:Script>   
        <![CDATA[   
            private var keys:Object = {};   
            private function init():void {   
                var arr:Array = [1,1,7,4,4,5,6];    
                var filteredArr:Array = arr.filter(removedDuplicates);   
                  
                arrColl.source = arr;   
                dedupedArrColl.source = filteredArr;   
            }
           
            private function removedDuplicates(item:Object, idx:uint, arr:Array):Boolean {   
                if (keys.hasOwnProperty(item)) {   
                    return false;   
                } else {   
                    keys[item] = item;   
                    return true;   
                }   
            }   
        ]]>   
    </mx:Script>   
      
    <mx:ArrayCollection id="arrColl" />   
    <mx:ArrayCollection id="dedupedArrColl" />   
      
    <mx:HBox>   
        <mx:VBox>   
            <mx:Label text="Original ({arrColl.length} items):" />   
            <mx:List dataProvider="{arrColl}" />   
        </mx:VBox>   
        <mx:VBox>   
            <mx:Label text="Filtered ({dedupedArrColl.length} items):" />   
            <mx:List dataProvider="{dedupedArrColl}" />   
        </mx:VBox>   
    </mx:HBox>   
      
</mx:Application>