4 Replies Latest reply on Feb 9, 2010 1:27 PM by cyber0897

    is there a way you can add elements dynamically to an existing array??

    cyber0897 Level 1

      hey guys... i need to add elements to an array dynamically... how do i do that?

       

      for example... in one of my functions i do..

       

           for each(var item:Object in fileList){
                fileListArr.push(item);
           }
      

       

       

      and in a later function i need to add one more element to my fileListArr...

      so i tried doing

       

      fileListArr[indexNum].push({
           key:videoKey
      });
      

       

       

      so i need to have an array which resembles something like this...

       

       

      Before adding elements...

      fileListArr:

           [0]:  name:test1

                  Size:12K

                  caption:testing caption

                  number:1

           [1]:  name:test2

                  Size:12K

                  caption:testing caption

                  number:2

       

       

      after adding key to array

       

      fileListArr:

           [0]:  name:test1

                  Size:12K

                  caption:testing caption

                  number:1

                  key:xyxyyy11y1yy1y1y2y2u33n

           [1]:  name:test2

                  Size:12K

                  caption:testing caption

                  number:2

                  key:iiduudjmenri112jj2n4n3m2j1j21

       

       

       

      any ideas?

        • 1. Re: is there a way you can add elements dynamically to an existing array??
          rfkrocktk Level 1

          I think you're doing it right. This is how I'd do what you're talking about when looping over an Array:

           

          for each (var object:Object in fileListArr) {
              object.key = "abcdefg";
          }
          

           

          The only problem with your example is that if you're using an Array rather than, say, an ArrayList or an ArrayCollection is that no data-binding will be able to take place. Thus, you may not see any visible changes if you're trying to visually update items. Instead, I'd suggest using ArrayList. Plus, it seems as if you're using regular Objects which are also not bindable by default, so you'll probably still have that problem unless you switch over to using ObjectProxy instances. Eg:

           

          fileArrayList = new ArrayList();
          for (var i:uint = 0; i < fileList.length; i++) {
              fileArrayList.addItem(new ObjectProxy(fileList.getItemAt(i)));
          }
          // then, later on...
          for (var i:uint = 0; i < fileArrayList.length; i++) {
              fileArrayList.getItemAt(i).key = value;
          }
          

          1 person found this helpful
          • 2. Re: is there a way you can add elements dynamically to an existing array??
            cyber0897 Level 1

            hmm interesting... so i made the changes... i changed the array to arraylist and the code i have is as follows... but i still get an error... the error says ... "ReferenceError: Error #1056: Cannot create property key on flash.net.FileReference."

             

             

            public var videoReference:VideoHandler;
            public var fileRef:FileReferenceList = new FileReferenceList();
            [Bindable] public var fileListArr:ArrayList = new ArrayList();
            [Bindable] public var fileNames:ArrayCollection = new ArrayCollection();
            
            
            
            public function selectionHandler(event:Event):void{
                 fileRef.removeEventListener(Event.SELECT, selectionHandler);
                 var numSelected:int = event.target.fileList.length;
                 var fileList:Array = event.target.fileList;
                 
                 for each(var item:Object in fileList){
                      fileListArr.addItem(item);
                      fileNames.addItem({
                           num: fileNames.length + 1,
                           name: item.name,
                           size: formatFileSize(item.size),
                           status: ""
                      });
                 }
                 var newListLength:Number = fileListArr.length;
                 if(fileCounter > 0){
                      loopList(fileCounter);
                 }
                 else
                      loopList(0);
            }
            
            
            public function loopList(value:int):void{
                 //trace("looplist -->");
                 if(value < fileListArr.length){
                      _numCurrentUpload = value;
                      
                      file = new FileReference();
                      file = FileReference(fileListArr.getItemAt(value));
                      file.addEventListener(Event.COMPLETE, loadVideo);
                      file.addEventListener(ProgressEvent.PROGRESS, fileProgress);
                      file.load();
                 }
            }
            
            
            
            
            
            
            public function setUploadKey(event:ResultEvent):void{
                 if(event.result.ThereWasAnError){
                      Alert.show(event.result.ErrorMessages[0]);
                 }else{
                      videoKey = event.result.UploadKey;
            
                      if(fileCounter >= fileListArr.length){
                           trace("in if");
                           fileCounter = 0;
                           uploadLoopList(fileCounter);
                      }else{
                           trace("in else");
                           //fileListArr[fileCounter - 1]['videoKey'] = videoKey;
            
             -----> get an error here --->fileListArr.getItemAt(fileCounter - 1).key = videoKey;
            
                           //fileListArr[fileCounter - 1] = [{key: videoKey}];
                           
                           loopList(fileCounter);
                      }
                 }
            }
            
            • 3. Re: is there a way you can add elements dynamically to an existing array??
              rfkrocktk Level 1

              FileReference is not a dynamic class, so you can't dynamically declare

              properties like "key" on it. You'll have to create your own data type for

              storing this information. If you're unfamiliar with this, you should learn

              ActionScript 3. A great help to me which taught me AS3 is the book

              "Essential ActionScript 3" by Colin Moock. You'll learn a whole lot and

              it'll make you a superb Flex/ActionScript developer.

              1 person found this helpful
              • 4. Re: is there a way you can add elements dynamically to an existing array??
                cyber0897 Level 1

                hmm i c... yeah i dont know how to create myown data types... i didnt even know i could do that but i'll look that up in the book... thanks a lot for you help tho... i just decided to create a parallel array for now..