1 Reply Latest reply on Jun 23, 2013 12:17 AM by the0bot

    How to convert Array 2D to Array collection in Flex

    Rohini_1327

      I have a 2D array as follows:

      Asia  India  Chennai  TN  Category1 Product1 100
      Asia  India  Mumbai   MH  Category1 Product1 100
      Asia  India  Calcutta CT  Category1 Product1 100
      Asia  India  Calcutta CT  Category2 Product2 200
      EMEA  UK     London   LN  Category3 Product1 123    
      EMEA  UK     London   LN  Category3 Product2 455    
      EMEA  UK     Reading  RN  Category1 Product1 500    
      EMEA  UK     Reading  RN  Category1 Product2 430

      How can I convert the above 2D Array to Array collection so that I  can access each data with individual attribute like the first column to  indicate attribute as "continent",2nd column as "country" etc .,  arrayCollection haveing the structure as {Continent:"Asia",  Country:"India",...etc} and so on.How to achieve it dynamically.

        • 1. Re: How to convert Array 2D to Array collection in Flex
          the0bot Level 1

          Hi Rohini_1327,

           

          Your question is not very clear. Since you refer to it as a 2d array, I take it you mean you have an array of arrays, such that:

           

          myArray:Array = [("Asia", "India", "Chennai", "TN", "Category1", "Product1", 100),

               ("Asia", "India", "Mumbai", "MH", "Category1", "Product1", 100),

               ("Asia", "India", "Calcutta", "CT", "Category1", "Product1", 100),

               ("Asia", "India", "Calcutta", "CT", "Category2", "Product2", 200),

               ("EMEA", "UK", "London", "LN", "Category3", "Product1", 123),

               ("EMEA", "UK", "London", "LN", "Category3", "Product2", 455),

               ("EMEA", "UK", "Reading", "RN", "Category1", "Product1", 500),

               ("EMEA", "UK", "Reading", "RN", "Category1", "Product2", 430)];

           

           

           

          if so, since you already know the category names and everything is all nice and neat:

           

           

          var types:Array = ["continent", "country", "city", "abbr", "cat1", "prod1", "qty"];  // an array of names for the columns

           

          var myArrayCollection:ArrayCollection = new ArrayCollection;

           

          for each(var arr:Array in myArray)    //  iterate through each elment of the 2d array

          {

                 var temp:Object = new Object;  // a temporary holder for our arrayCollection items

                for(var i:int = 0; i < 7; i++)          //  iterate through  each sub-array

                {

                       temp[types(i)] = arr(i);        

                 }                                                    

                 myArrayCollection.addItem(temp);

          }

           

           

          now:  myArrayCollection[0].continent = "Asia"

           

          or:  var obj:Object = myArrayCollection.getItemAt(3);

          obj.city = "Calcutta"

           

                 

          You have to iterate through the list. In this case, with a 2d array, you have 2 nested iterations. If each array element of myArray was also an array, you simply add another iteration to the function. 

           

          See http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7fdc.html for more information.