2 Replies Latest reply on Sep 8, 2009 8:53 AM by bk843746

    Data grouping

    bk843746

      Hi

       

      Is there an easy way to convert variable one into variable two?

       

      var one:ArrayCollection = new ArrayCollection([

        {type:"1", name:"a", value:"test1"},

        {type:"1", name:"b", value:"test2"},

        {type:"2", name:"a", value:"test3"},

        {type:"3", name:"a", value:"test4"}

      ]);

       

      var two:ArrayCollection = new ArrayCollection([

        {name: "1", children: [

          {name:"a", value:"test1"},

          {name:"b", value:"test2"}

        ]},

        {name: "2", children: [

          {name:"a", value:"test3"}

        ]},

        {name: "3", children: [

          {name:"a", value:"test4"}

        ]}

      ]);

       

      It's fine if variable two still has the type method in the child records.

       

      Thanks

        • 1. Re: Data grouping
          Andrew Rosewarn Level 3

          Hi

           


          Well unless anyone else knows of some functions....  This works

           

          <?xml version="1.0"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="makeNewArray();">
             
              <mx:Script>
                  <![CDATA[
                      import mx.collections.ArrayCollection;
                     
                      private var groupList:Array;
                     
                      private var one:ArrayCollection = new ArrayCollection([
                      {type:"1", name:"a", value:"test1"},
                      {type:"1", name:"b", value:"test2"},
                      {type:"2", name:"a", value:"test3"},
                      {type:"3", name:"a", value:"test4"}

           

                      ]);
                     
                      private function createGroupList():void {
                          groupList = new Array();
                          var alreadyAdded:Boolean;
                          groupList.push(one.getItemAt(0).type);
                          for (var i:int = 1; i<one.length; i++) {
                              alreadyAdded = false;
                              var type:String = one.getItemAt(i).type;
                              for (var j:int = 0; j<groupList.length; j++) {
                                  if (groupList[j] == type) {
                                      alreadyAdded = true;
                                      break;
                                  }
                              }
                              if (!alreadyAdded) {groupList.push(type)};
                          }
                      }
                     
                     
                      private function makeNewArray():void {
                          // Create a unique group list
                          createGroupList();
                         
                          // Create new arrayCollection
                          var two:ArrayCollection = new ArrayCollection();
                         
                          // Loop through unique group list
                          for (var i:int =0; i<groupList.length; i++) {
                              var type:Object = new Object();
                              type.name = groupList[i];
                             
                              // Loop through the orginial one array
                              var children:Array = new Array();
                              for each (var obj:Object in one) {
                                 
                                  trace (groupList[i] + ' : ' + obj.type)
                                  if (groupList[i].toString() == obj.type.toString()) {
                                      var child:Object = new Object();
                                      child.name = obj.name;
                                      child.value = obj.value;
                                      children.push(child);
                                  }
                              }
                              type.children = children;
                              two.addItem(type);
                          }
                      }
                     
                  ]]>
              </mx:Script>
          </mx:Application>

           

          Hope it helps

           

          Andrew

          • 2. Re: Data grouping
            bk843746 Level 1

            Hi

             

            I was looking at the GroupingCollection but I couldn't get it to work correctly. Would this class or any other classes in the mx.collections namespace do the job?