4 Replies Latest reply: May 22, 2013 10:02 AM by Vinodhan-Selvam RSS

    ArrayCollection remove duplicates and add totals

    MrFork Community Member

      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

        • 2. Re: ArrayCollection remove duplicates and add totals
          RK...

          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"]);
              }
             }

          • 3. Re: ArrayCollection remove duplicates and add totals
            MrFork Community Member

            THANK YOU! that works perfectly, many many thanks. If I was with you now, I'd buy you a beer..or 2

            • 4. Re: ArrayCollection remove duplicates and add totals
              Vinodhan-Selvam

              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..