Skip navigation
MrFork
Currently Being Moderated

ArrayCollection remove duplicates and add totals

Nov 13, 2009 5:58 AM

Does anyone have a function that removes duplicates from an array collection and totals up a value where the duplicates exist?

 

E.g. Array

[Name: John, Value: 2]

[Name: Dave, Value: 10]

[Name: John, Value: 2]

[Name: Dave, Value: 8]

[Name: Dave, Value: 2]

[Name: John, Value: 1]

 

// Output should be:

 

Name: John Value: 5

Name: Dave Value: 20

 

Many thanks - I really need some help with this, I just cant get the logic.

 

Thanks

 
Replies
  • Currently Being Moderated
    Nov 13, 2009 7:39 AM   in reply to MrFork

    Please try this,

     

       private function filterData(event: MouseEvent): void {
        var source: ArrayCollection = new ArrayCollection();
        source.addItem({name:"John", value:2});
        source.addItem({name:"Dave", value:10});
        source.addItem({name:"John", value:2});
        source.addItem({name:"Dave", value:8});
        source.addItem({name:"Dave", value:2});
        source.addItem({name:"John", value:1});
       
        var result: ArrayCollection = new ArrayCollection();
        var flag: Boolean;
        for (var i: int = 0; i < source.length; i++) {
         flag = false;
         for (var j: int = 0; j < result.length; j++) {
          if (result.getItemAt(j)["name"] == source.getItemAt(i)["name"]) {
           result.getItemAt(j)["value"] = (result.getItemAt(j)["value"] + source.getItemAt(i)["value"]);
           flag = true;
           break;
          }
         }
         if (!flag) {
          result.addItem(source.getItemAt(i));
         }
        }
        for (var k: int = 0; k < result.length; k++) {
         Alert.show(result.getItemAt(k)["name"] + " -- " + + result.getItemAt(k)["value"]);
        }
       }

     
    |
    Mark as:
  • Currently Being Moderated
    May 22, 2013 10:02 AM   in reply to MrFork

    Hi MrFork i tryed with this..

     

    private function filterData(event: MouseEvent): void {

        var source: ArrayCollection = new ArrayCollection();

        source.addItem({name:"John", value:2});

        source.addItem({name:"Dave", value:10});

        source.addItem({name:"John", value:2});

        source.addItem({name:"Dave", value:8});

        source.addItem({name:"Dave", value:2});

        source.addItem({name:"John", value:1});

     

    for each(var previousValue:Object in source)

                                  {

                                            for each(var newValue:Object in source)

                                            {

                                                      if( previousValue== newValue)

                                                                continue;

                                                      if( previousValue.name== newValue.name)

                                                      {

                                                                var indx:int = source.getItemIndex(newValue);

                                                                if(indx > 0)

                                                                          source.removeItemAt(indx);

                                                      }

                                            }

                                  }

                                  source.refresh();

    }


    for me its working fine.. 

      

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points