//takes an AC and the filters out all duplicate entries
public function getUniqueValues (collection : ArrayCollection) : ArrayCollection {
var length : Number = collection.length;
var dic : Dictionary = new Dictionary();
//this should be whatever type of object you have inside your AC
var value : Object;
for(var i : int= 0; i < length; i++){
value = collection.getItemAt(i);
dic[value] = value;
}
//this bit goes through the dictionary and puts data into a new AC
var unique = new ArrayCollection();
for(var prop:String in dic){
unique.addItem(dic[prop]);
}
return unique;
}
var ar:Array = ["Joe","Bob","Curl","Curl"];
var distinctData = ar.filter(function(itm, i){
return ar.indexOf(itm)== i;
});
Alert.show(distinctData.join(","));
或者更好
Array.prototype.distinct = function():*
{
var arr:Array = this as Array;
return arr.filter(function(itm, i){
return (this as Array).indexOf(itm)== i;
},arr);
};
var ar:Array = ["Joe","Bob","Curl","Curl"];
Alert.show(ar.distinct());
function removeDuplicateElement(_arr:Array):Array{
//set new Dictionary
var lDic:Dictionary = new Dictionary();
for each(var thisElement:* in _arr){
//All values of duplicate entries will be overwritten
lDic[thisElement] = true;
}
_arr = [];
for(var lKey:* in lDic){
_arr.push(lKey);
}
return _arr;
}
5条答案
按热度按时间14ifxucb1#
应该比另一个解决方案更简单。
s4n0splo2#
这是我快速搜索后发现的。
pgpifvop3#
如果你找到了数组的解,你可以对ArrayCollection做同样的事情。你可以改变
arrayCollection.source
,arrayCollection
也会被改变。一般来说,我们可以假设ArrayCollection是数组的 Package 器。e1xvtsh34#
数组包含一个过滤函数,我们可以如下使用它。
或者更好
9wbgstp75#