2 Replies Latest reply on Sep 3, 2010 10:41 AM by superkatakam

    Eliminating duplicates in arraycollection with multiple values in them

    superkatakam Level 1

      I have an array collection with 4 values in it. It doesnt have a key but each row in the array collection has to be different.

      Could someone help me out with the code to eliminate all the duplicates?

      I have the array collection

      anchorGroupDetailAC  and each object in the array collection has anchorGroupId, assignmentStep, dimensionMasterId and dimensionValueId.

       

      i need each cell in the anchorGroupDetail to be unique. I found some examples online that talk about using one element in the object to be a key but how can we do it for all the 4 elements combined?

       

      Your help is greatly appreciated.

      Thank you

      Nikhil

        • 1. Re: Eliminating duplicates in arraycollection with multiple values in them
          krafttimd Level 2

                  private function cc(event:FlexEvent) : void {
                      for(var i:int = 0; i < 4; i++) {
                          var obj:Object = new Object();
                          if(i < 3) {
                              obj['anchorGroupId'] = i;
                              obj['assignmentStep'] = ("step" + i);
                              obj['dimensionMasterId'] = (i + 1) * 2;
                              obj['dimensionValueId'] = (i + 1) * 3;
                          } else {
                              obj['anchorGroupId'] = i - 1;
                              obj['assignmentStep'] = ("step" + (i - 1));
                              obj['dimensionMasterId'] = i * 2;
                              obj['dimensionValueId'] = i * 3;
                          }
                          _test.addItem(obj);
                      }
                  }

           

                  private function filter() : void {

           

                      for(var i:int = 0; i < _test.length; i++) {
                          if(getNbrTimesInList(Object(_test.getItemAt(i))) > 1) {
                              _test.removeItemAt(i);
                              i--;
                          }
                      }
                  }

           

                  private function getNbrTimesInList(object:Object) : int {
                      var nbr:int = 0;
                      for each (var obj:Object in _test) {
                          if(   obj['anchorGroupId'] == object['anchorGroupId']
                              && obj['assignmentStep'] == object['assignmentStep']
                              && obj['dimensionMasterId'] == object['dimensionMasterId']
                              && obj['dimensionValueId'] == object['dimensionValueId'] ) {
                              nbr++;
                          }
                      }

           

                      return nbr;
                  }

           

                  private var _test:ArrayCollection = new ArrayCollection();
                  private function handleClick():void {
                      trace("collection before filter: " + ObjectUtil.toString(_test));
                      filter();
                      trace("collection after filter: " + ObjectUtil.toString(_test));
                  }

           

          Trace Result:

           

          collection before filter: (mx.collections::ArrayCollection)#0
            filterFunction = (null)
            length = 4
            list = (mx.collections::ArrayList)#1
              length = 4
              source = (Array)#2
                [0] (Object)#3
                  anchorGroupId = 0
                  assignmentStep = "step0"
                  dimensionMasterId = 2
                  dimensionValueId = 3
                [1] (Object)#4
                  anchorGroupId = 1
                  assignmentStep = "step1"
                  dimensionMasterId = 4
                  dimensionValueId = 6
                [2] (Object)#5
                  anchorGroupId = 2
                  assignmentStep = "step2"
                  dimensionMasterId = 6
                  dimensionValueId = 9
                [3] (Object)#6
                  anchorGroupId = 2
                  assignmentStep = "step2"
                  dimensionMasterId = 6
                  dimensionValueId = 9
              uid = "DC1E4607-4E77-90E9-D189-D87022CF6189"
            sort = (null)
            source = (Array)#2
          collection after filter: (mx.collections::ArrayCollection)#0
            filterFunction = (null)
            length = 3
            list = (mx.collections::ArrayList)#1
              length = 3
              source = (Array)#2
                [0] (Object)#3
                  anchorGroupId = 0
                  assignmentStep = "step0"
                  dimensionMasterId = 2
                  dimensionValueId = 3
                [1] (Object)#4
                  anchorGroupId = 1
                  assignmentStep = "step1"
                  dimensionMasterId = 4
                  dimensionValueId = 6
                [2] (Object)#5
                  anchorGroupId = 2
                  assignmentStep = "step2"
                  dimensionMasterId = 6
                  dimensionValueId = 9
              uid = "DC1E4607-4E77-90E9-D189-D87022CF6189"
            sort = (null)
            source = (Array)#2


          This is not pretty...but it works.

          T.K.