开发者

Removing duplicates in Flex 4 XMLList with filterfunction

开发者 https://www.devze.com 2023-03-08 04:58 出处:网络
I have seen numerous examples on how to remove duplicates in ArrayCollection but I can\'t seem to transpose this to XMLList. In most ArrayCollection examples, the example compares the key of the array

I have seen numerous examples on how to remove duplicates in ArrayCollection but I can't seem to transpose this to XMLList. In most ArrayCollection examples, the example compares the key of the array with hasOwnProperty method and return a bool. That's fine, but what would I compare to when using an XMLList? Let's say I have:

<fx:XML id="testXML" xmlns="">
   <universe>
      <category cname="cat 1">
         <item iname = "All"/>
         <item iname = "item 1"/>
         <item iname = "item 2"/>
      </category>
      <category cname="cat 2">
         <item iname = "All"/>
         <item iname = "item 3"/>
         <item iname = "item 4"/>
      </category>
   </universe>
</fx:XML>

[actionscript]

var myList:XMLList = testXML..@iname;

will give two occurences of the item "ALL". I know I might have to convert the XMLList to an XMLListCollection to use the filterFunction (how would I go about doing that - or should I just define myList as an XMLListCollection right from the start). then on to the filterFunction:

private function remove Duplicate (item:Object): Boolean
{
   here I don't know how to compare the item to tell me if the object already exist
   or not. I guess I need to compare the item to a copy of the list and see if the
   item has already been seen in the copy of the list. Or is there a clean way to
   do this?
}

then all this is passed to a dropDownList:

<s:DropDownList id="myDDL" dataProvider="{myList}" /&开发者_JAVA百科gt;


Using E4x filter function you can for example put the key into an Object (if the key is a String, otherwise use a dictionary instead of an Object) and look if it is already present to build your XMLList :

var xml:XML=<universe>
      <category cname="cat 1">
         <item iname="All"/>
         <item iname="item 1"/>
         <item iname="item 2"/>
      </category>
      <category cname="cat 2">
         <item iname="All"/>
         <item iname="item 3"/>
         <item iname="item 4"/>
      </category>
   </universe>

       function filter(xml:XML):XMLList {
        var seen:Object={}
        return xml..@iname.(!seen[valueOf()]&&(seen[valueOf()]=true))
       }

trace( filter(xml)  )

Here a live example at wonderfl : http://wonderfl.net/c/10xr


The easiest way I know how to do this is to use ActionLinq. This code will take your e4x code and turn it into an Enumerable, cast the attributes as strings, make the items in the list distinct and dump it out as an ArrayCollection.

myList = Enumerable.from(testXML..@iname)
         .cast(String)
         .distinct()
         .toArrayCollection();

If you don't want to use ActionLinq, you can implement this using a Dictionary:

[Bindable]
private var myList:ArrayList;

private function removeDuplicates(data:XMLList):ArrayList {
    var result:ArrayList = new ArrayList();
    var found:Dictionary = new Dictionary();

    for each(var item:String in data) {
        if(item in found) {
            continue;
        }

        found[item] = true;
        result.addItem(item);
    }

    return result;
}

And then when the XML is ready, you can call it:

myList = removeDuplicates(testXML..@iname);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号