Recipe 5.4. Removing Elements


Problem

You want to remove one or more elements from an array and shift any remaining elements to fill the vacant indexes.

Solution

Use the splice( ) method to remove elements from the middle of the array. Use pop( ) to remove the last element or shift( ) to remove the first element.

Discussion

Remove elements from an array by starting at a specified index using the splice( ) method. When using splice( ) to delete elements, you should pass it two parameters:


start

The index of the array from which to start deleting elements.


deleteCount

The number of elements to delete. If this value is undefined, all the elements from start to the end of the array are deleted:

var letters:Array = ["a", "b", "c", "d"];       // Remove one element from letters starting at index 1. letters.splice(1, 1);       // Display the results. The array now contains three elements: // "a", "c", and "d". for (var i:int = 0; i < letters.length; i++) {     trace(letters [i]); }

The splice( ) method also returns a new array containing the deleted elements; for example:

var letters:Array = ["a", "b", "c", "d"];       // Remove two elements from letters starting at index 0. var deleted:Array = letters.splice(0, 2);       // Display the deleted elements: "a" and "b". for (var i:int = 0; i < deleted.length; i++) {     trace(deleted[i]); }

To delete a single element from the beginning or end of the array, you can use the shift( ) and pop( ) methods. The shift( ) method removes the first element of the array and returns its value. The pop( ) method removes the last element of the array and returns its value:

var letters:Array = ["a", "b", "c", "d"];       // Remove the first element and display its value. trace(letters.shift(  ));       // Remove the last element and display its value. trace(letters.pop(  ));       // Display the remaining elements.  // The array has two elements left: "b" and "c". for (var i = 0; i < letters.length; i++) {     trace(letters[i]); }

When you remove elements from an array in a for statement, you need to change the value of the index variable accordingly. The following example illustrates what can happen if you don't update the value of the index variable:

var numbers:Array = new Array(4, 10); numbers[4] = 1; trace(numbers);  // Displays: 4,10,undefined,undefined,1 for(var i:int = 0; i < numbers.length; i++) {     if(numbers[i] == undefined) {         numbers.splice(i, 1);     } } trace(numbers);  // Displays: 4,10,undefined,1

In the preceding code, you might have expected it to remove both of the undefined elements from the array. However, as shown in the final trace, it removed only one. If you go through the for statement step-by-step, you can see why:

  1. The first two iterations do nothing because the elements are not undefined.

  2. The third iteration sees that the third element is undefined and removes it. At that point, the fourth and fifth elements shift down by one index, becoming the third and fourth elements.

  3. The next iteration checks the new fourth element, which is now the last. It skips right over the other undefined element (now third). Instead, you can make sure you decrement the index variable after removing the element. The following code shows how you might do that:

  4. var numbers:Array = new Array(4, 10); numbers[4] = 1; trace(numbers);  // Displays: 4,10,undefined,undefined,1 for(var i:int = 0; i < numbers.length; i++) {   if(numbers[i] == undefined) {     numbers.splice(i, 1);     i--;   } } trace(numbers);  // Displays: 4,10,1




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net