apache-flex ActionScript 3 -移除ArrayCollection中的重复项目

xxls0lw8  于 2022-11-01  发布在  Apache
关注(0)|答案(5)|浏览(126)

我有一个用户名和用户ID的列表的ArrayCollection。在这个列表中有重复的,我需要删除。我已经搜索了互联网,虽然有很多这样的例子使用数组,我不能找到任何明确的例子使用ArrayCollection的。

14ifxucb

14ifxucb1#

应该比另一个解决方案更简单。

function removeDuplicatesInArray(val:*, index:uint, array:Array):Boolean {
  return array.indexOf(val) == array.lastIndexOf(val);
}

function removeDuplicatesInCollection(collection:ArrayCollection):ArrayCollection {
  collection.source = collection.source.filter(removeDuplicatesInArray);

  return collection;
}
s4n0splo

s4n0splo2#

这是我快速搜索后发现的。

//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;
}
pgpifvop

pgpifvop3#

如果你找到了数组的解,你可以对ArrayCollection做同样的事情。你可以改变arrayCollection.sourcearrayCollection也会被改变。一般来说,我们可以假设ArrayCollection是数组的 Package 器。

e1xvtsh3

e1xvtsh34#

数组包含一个过滤函数,我们可以如下使用它。

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());
9wbgstp7

9wbgstp75#

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;
}

相关问题