Search code examples
actionscript-3flashapache-flexactionscriptarraycollection

How can I remove duplicate names?


Consider the follwoing code:

The follwoing code print processorName in this format: DC5,DR2,DR3 But when it comes to duplicates it prints as DR3DR3. I need to remove the duplicate processorName. I am not able to figure it out, how can I do this?

 public function processorNameFormat(item:Object, column:GridColumn):String
{
    var processorNames:String = "";
    if (!(item is ProcessOrderDO)) {
        return "";
    }
    var poDestReqList:ArrayCollection = item.processOrderDestinationRequirementDOList ;

    for each(var destReq:ProcessOrderDestinationRequirementDO in poDestReqList)
    {
        if(destReq.processorOID == (poDestReqList[poDestReqList.length-1] as ProcessOrderDestinationRequirementDO).processorOID)
            processorNames +=  destReq.processorName ;
        else
            processorNames +=  destReq.processorName+"," ;
    }
    return processorNames;
}

Solution

  • If you want to remove duplicate entries from the result, you should do like this:

    public function processorNameFormat(item:Object, column:GridColumn):String
    {
        var processorNames:String = "";
        if (!(item is ProcessOrderDO)) {
            return "";
        }
        var d:Dictionary=new Dictionary();
        var poDestReqList:ArrayCollection = item.processOrderDestinationRequirementDOList ;
    
        for each(var destReq:ProcessOrderDestinationRequirementDO in poDestReqList)
        {
            var s:String=destReq.processorName;
            if (d[s]) continue;
            d[s]=true; // fill the dictionary with values to catch duplicates
            if (processorNames=="")
                processorNames +=  destReq.processorName ;
            else
                processorNames +=  ","+destReq.processorName;
        }
        return processorNames;
    }
    

    You make a Dictionary containing found previous processor names, and if there would me a match in the next retrieved processor name, it will be skipped, otherwise added to both dictionary and result string.