2 Replies Latest reply on May 13, 2011 10:11 AM by GordonSmith

    HOW TO REMOVE NUMEROUS ITEMS FROM A COLLECTION AT ONCE

    southlondon Level 1

      Hi All,

      What is the best way to remove a range of items from an ArrayCollection at once ? Using a for loop does not work, it seems to miss one or a couple of the target collection items. Thanks in advance

       

      for

       

       

      (var a:int = 0; a < payLoad.length; a++)

      {

       

             if(a > 1 && a < 5)

           {

                 payLoad.removeItemAt(a);

             }

      }

        • 1. Re: HOW TO REMOVE NUMEROUS ITEMS FROM A COLLECTION AT ONCE
          pauland Level 4

          southlondon wrote:

           

          Hi All,

          What is the best way to remove a range of items from an ArrayCollection at once ? Using a for loop does not work, it seems to miss one or a couple of the target collection items. Thanks in advance

           

           

          for

           

           

          (var a:int = 0; a < payLoad.length; a++)

          {

           

           

                 if(a > 1 && a < 5)

               {

                     payLoad.removeItemAt(a);

                 }

          }

           

          for (var a:int = Math.Min(payLoad.length,5); a>1; a--)

          {

                     payLoad.removeItemAt(a);

          }

           

          Is an improvement. You must count downwards when removing items.

           

          Didn't look properly!

          • 2. Re: HOW TO REMOVE NUMEROUS ITEMS FROM A COLLECTION AT ONCE
            GordonSmith Level 4

            When you remove an item, the indexes of all subsequent items decrease by 1. Looping backwards makes this not matter, because you've already processed the subsequent items.

             

            Gordon Smith

            Adobe Flex SDK Team