1 Reply Latest reply on Dec 3, 2009 2:12 PM by MrFork

    remove duplicates and add up problem

    MrFork Level 1

      Hello All,

       

      I was given this code by a fellow member on this site but for some reason, there seems to be a rather strange binding error when using it. Please see the code below:

       

      Everytime I press the 'filterData' button, the whereFromData is being incremented everytime I press the button and therefore not giving the desired result. Could someone please help me as I have been pulling my hair out for hours wondering why this is happening.

       

      <?xml version="1.0" encoding="utf-8"?>

       

      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

       

          <mx:Script>

       

              <![CDATA[

       

              import mx.collections.ArrayCollection;

              import mx.rpc.events.ResultEvent;

              public var whereFromData:ArrayCollection = new ArrayCollection

                  private function recieveWhereFromData(evt:ResultEvent):void

                  {

                      var tmpArray:ArrayCollection = new ArrayCollection ();

                      tmpArray = evt.result.data.graphdata;

                      for each (var o:Object in tmpArray)

                      {

                          whereFromData.addItem({month:o.month,pagepath:o.pagepath,year:o.year,source:o.source,visi ts:o.visits});

                      }

                      test.dataProvider = whereFromData;

                  }

                  private function filterData():void

                  {

                      var tmpArray:Object = new Object;

                      tmpArray = whereFromData;

                      tmpArray = filterGoogleSourceData(tmpArray);

                      test.dataProvider = tmpArray;

                  }

                  private function filterGoogleSourceData(source:Object):Object{

                      var tmpArray:ArrayCollection = new ArrayCollection();

                      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)["source"] == source.getItemAt(i)["source"]) {

                         result.getItemAt(j)["visits"] = (result.getItemAt(j)["visits"] + source.getItemAt(i)["visits"]);

                         flag = true;

                         break;

                        }

                       }

                       if (!flag) {

                        result.addItem(source.getItemAt(i));

       

                       }

       

                      }

       

                      for (var l: int = 0; l <result.length; l++) {

       

                          tmpArray.addItem({source:result.getItemAt(l)["source"], visits:result.getItemAt(l)["visits"]});

                      }

                      return tmpArray;

                     }

              ]]>

       

          </mx:Script>

       

          <mx:Button x="124" y="10" label="Get Data" click="getWhereFrom.send()"/>

          <mx:Button x="114" y="210" label="Filter Data" click="filterData()"/>

          <mx:HTTPService id="getWhereFrom" showBusyCursor="true" result="recieveWhereFromData(event)" method="GET" url="http://localhost/write_test/get_wherefrom.php" useProxy="false"/>

       

          <mx:DataGrid id="test" x="83" y="49">

       

          </mx:DataGrid>

       

       

       

      </mx:Application>

        • 1. Re: remove duplicates and add up problem
          MrFork Level 1

          It looks like the function I was given has a rather strange binding abilitiy in that once used, it modifies all associated data arrays, even when the removeAll() function is used.

           

          Does anyone have any suggestions or another remove duplicates but total up the duplicated rows function? 

           

          private function filterGoogleSourceData(source:ArrayCollection):ArrayCollection{
                         
                          var tmpArray:ArrayCollection = new ArrayCollection();
                          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)["source"] == source.getItemAt(i)["source"]) {
                             result.getItemAt(j)["visits"] = (result.getItemAt(j)["visits"] + source.getItemAt(i)["visits"]);
                             flag = true;
                             break;
                            }
                           }
                           if (!flag) {
                            result.addItem(source.getItemAt(i));
                           }
                          }
                          for (var l: int = 0; l <result.length; l++) {
                              tmpArray.addItem({source:result.getItemAt(l)["source"], visits:result.getItemAt(l)["visits"]});
                          }
                          return tmpArray;
                         }