2 Replies Latest reply on Apr 27, 2009 5:58 AM by Barna Biro

    Assigning ArrayCollection items to a single Object

    HyderAlamgir Level 1

      I have an arraycollection of product_items

      product_item is an object with name, price, and description

       

      And I have an object dataRequest

      dataRequest values are assigned as such...

       

      dataRequest = ({ AMT: orderTotal, NAME: txiName.text, ... etc });

       

       

      Now how do I assign each product_item's name and price as separate elements of dataRequest...

       

      Like this,

       

      dataRequest = ({ AMT: orderTotal, NAME: txiName.text, 
                     ITEM1_NAME: product_items[0].name, ITEM1_PRICE: product_item[0].price,
                     ITEM2_NAME: product_items[1].name, ITEM2_PRICE: product_item[1].price,
                     ITEM3_NAME: product_items[2].name, ITEM3_PRICE: product_item[2].price,
                     ...
                     ...
                     ... till number of product_items in arraycollection });
      
        • 1. Re: Assigning ArrayCollection items to a single Object
          m_hartnett Level 3

          You can throw it into a loop and create the object;

           

           

           

           

           

           

           

           

           

           

           

          private

           

          function buildObj():void {

           

           

               var ac:ArrayCollection = new ArrayCollection()

           

               var o1:Object = new Object();

               o1.name =

           

          "O1Name"

           

           

           

           

              o1.price = 200.00

           

           

               var o2:Object = new Object()

               o2.name =

           

          "O2Name"

           

           

           

           

               o2.price = 1000.00

               ac.addItem(o1);

               ac.addItem(o2)

           

           

           

               var dataRequest:Object = new Object()

           

               for(var i:int = 0;i<ac.length;i++) {

           

                    dataRequest[

           

          "prodName" + i] = ac.getItemAt(i).name;

                    dataRequest[

           

          "prodPrice" + i] = ac.getItemAt(i).price;

           

               }

           

          }

           

           

           

          • 2. Re: Assigning ArrayCollection items to a single Object
            Barna Biro Level 3

            This is more of a pseudo-code so don't copy-paste and run it because it will surely not work but it will hopefully put you on the right track:

             

            package
            {
                import flash.display.Sprite;
            
            
                public class CollectionExample extends Sprite
                {
                    private var product_items:ArrayCollection = [];
                    private var _dataRequestObj:Object = new Object();
                    private var _parsedData:Object = new Object();
                   
                    public function CollectionExample()
                    {
                        _parsedData.AMT = orderTotal;
                        _parsedData.NAME = txiName.text;
                        _parsedData.ITEMS = new ArrayCollection();
                       
                        var itemDetails:Object;
                       
                        for each (var collectionItem:Object in product_items)
                        {
                            itemDetails = new Object();
                            itemDetails.name = collectionItem.name as String;
                            itemDetails.price = collectionItem.price as Number;
                            _parsedData.ITEMS.addItem(itemDetails);               
                        }
                    }
                }
            }

             

            It would not produce the exact structure you wanted but it should put you on the right track.

             

            Best regards,

            Barna Biro