flex 去除Array中的重复数据

<?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 = [];
                arr.push(1);
                arr.push(1);
                arr.push(7);
                arr.push(4);
                arr.push(4);
                arr.push(5);
                arr.push(6);
 
                /* Filter the original array and call the
                    removeDuplicates() function on each item
                    in the array. */
                var filteredArr:Array = arr.filter(removedDuplicates);
 
                arrColl.source = arr;
                dedupedArrColl.source = filteredArr;
            }
 
            /**
             * This method is used to filter an array so that no
             * duplicate items are created. It works by first
             * checking to see if a keys object already contains
             * a key equal to the current value of the item.data
             * value. If the key already exists, the  current item
             * will not be readded to the data provider. If the key
             * does not already exist, add the key to the keys
             * object and add this item to the data provider.
             */
            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>
posted @ 2009-10-20 11:56  zhych  阅读(1601)  评论(0编辑  收藏  举报