Posted By

imhugo on 03/11/08


Tagged

actionscript array


Versions (?)

Removes specific item from Array


 / Published in: ActionScript
 

  1. // Creates Array
  2. var myArray:Array = ["item1", "item2", "item3", "item4", "item5"];
  3.  
  4. // Function
  5. function removeItemFromArray( item:String ):Void
  6. {
  7. trace("Array Original: " + myArray);
  8.  
  9. // Array Lenght
  10. var arrayLenght:Number = myArray.length;
  11.  
  12. // Searches item in array
  13. for ( i =0; i<arrayLenght; i++ )
  14. {
  15. // Finds item and removes it
  16. if ( myArray[i] == item )
  17. {
  18. myArray.splice( i, 1 );
  19. trace("Item Removed: " + myArray[i] + newline);
  20. trace("Array Updated: " + myArray);
  21. }
  22. }
  23. }
  24.  
  25. // Calls Function
  26. removeItemFromArray( "item3" );

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: cereals on May 21, 2008

You should go in reverse that way you can supply more than one item to remove!

var myArray:Array = ["item1", "item2", "item3", "item4", "item5", "item3"];

// Function function removeItemFromArray( item:String ):Void { trace("Array Original: " + myArray);

// Array Lenght
var i:Number = myArray.length - 1;

// Searches item in array
for ( i; i>=0; i-- )
{
    // Finds item and removes it
    if ( myArray[i] == item )
    {
       myArray.splice( i, 1 );
       trace("Item Removed: " + item );
       trace("Array Updated: " + myArray);
    } 
}

}

// Calls Function removeItemFromArray( "item3" );

Posted By: cereals on May 21, 2008

You should go in reverse that way you can supply more than one item to remove!

var myArray:Array = ["item1", "item2", "item3", "item4", "item5", "item3"];

// Function function removeItemFromArray( item:String ):Void { trace("Array Original: " + myArray);

// Array Lenght
var i:Number = myArray.length - 1;

// Searches item in array
for ( i; i>=0; i-- )
{
    // Finds item and removes it
    if ( myArray[i] == item )
    {
       myArray.splice( i, 1 );
       trace("Item Removed: " + item );
       trace("Array Updated: " + myArray);
    } 
}

}

// Calls Function removeItemFromArray( "item3" );

You need to login to post a comment.