?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 {
/* Create a dummy data source with some semi-random data. */
var arr:Array = [];
arr.push({data:1, label:"one"});
arr.push({data:1, label:"one"});
arr.push({data:1, label:"one"});
arr.push({data:1, label:"one"});
arr.push({data:2, label:"two"});
arr.push({data:2, label:"two"});
arr.push({data:2, label:"two"});
arr.push({data:1, label:"one"});
arr.push({data:3, label:"three"});
arr.push({data:3, label:"three"});
/* 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;
}
private function removedDuplicates(item:Object, 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 doesN'T already have this key, so add this item 2 the new dataprovider*/
keys[item.data] = 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>
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment