First, I am not sure of your problem.  Is the loop not working?  Or are you
just looking for an explanation?

 

Since you break when the item is found, I don’t think this is a problem for
you, but one issue with deleting multiple items in a loop is that the first
deletion causes the indexes to recalculate, confusing the loop counter.

 

One solution is to do the loop in reverse, deleting from the end.  This way,
the unprocessed indexes are not changed.

 

>From memory:

for( i = _collection.length-1; i >=0; i--) {
  if (collection [i] == item) {
    _collection.splice(i, 1);
    break;
  }

….

Tracy Spratt,

Lariat Services, development services available

  _____  

From: [email protected] [mailto:[email protected]] On
Behalf Of Ballai Tibi
Sent: Friday, December 04, 2009 1:48 PM
To: [email protected]
Subject: [SPAM] [flexcoders] am a newbie , can somebody explains to me this
simple For loop statement?

 

  

public function removeItem(item:StoreItem):void {
    
    var i:uint;  //declares a local variable i of type uint (unsigned int)

    /*initialize i with the value 0;   
      until i is smaller than the collection's length, execute the code
inside the for loop and increment i by one at the end of each step (i++)
    */
    for (i=0; i < _collection.length; i++){  
    
        //check if the item in the _collection array at position i is equal
to the item that was passed as an argument
        if (_collection [i]==item){
            
            //remove 1 item from the _collection array starting at position
i 
            _collection.splice(i, 1);
        
            //since the item we were looking for was found, there's no need
to continue searching
            break;
         }

    }

}



Tibor.



Reply via email to