ActionScript 3.0 Cookbook: Chapter 5, Arrays
Pages: 1, 2, 3, 4, 5, 6, 7, 8

Section 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

This excerpt is from ActionScript 3.0 Cookbook. Well before Ajax and Windows Presentation Foundation, Macromedia Flash provided the first method for building "rich" web pages. Now, Adobe is making Flash a full-fledged development environment, and learning ActionScript 3.0 is key. That's a challenge for even the most experienced Flash developer. This Cookbook offers more than 300 solutions to solve a wide range of coding dilemmas, so you can learn to work with the new version right away.

buy button

Section 5.5: Inserting Elements in the Middle of an Array

Problem

You want to insert elements in the middle of an array.

Solution

Use the splice( ) method.

Discussion

You can use the splice( ) method to insert elements as well as delete them. Values passed to the splice( ) method after the first and second parameters are inserted into the array at the index specified by the start parameter; all existing elements following that index are shifted up to accommodate the inserted values. If 0 is passed to the splice( ) method for the deleteCount parameter, no elements are deleted, but the new values are inserted:

var letters:Array = ["a", "b", "c", "d"];
     
// Insert three string values ("one", "two", and "three")
// starting at index 1.
letters.splice(1, 0, "r", "s", "t");
     
// letters now contains seven elements:
// "a", "r", "s", "t", "b", "c", and "d".
for (var i:int = 0; i < letters.length; i++) {
    trace(letters[i]);
}

You can also delete elements and insert new elements at the same time:

var letters:Array = ["a", "b", "c", "d"];
     
// Remove two elements and insert three more 
// into letters starting at index 1.
letters.splice(1, 2, "r", "s", "t");
     
// myArray now contains five elements:
// "a", "r", "s", "t", and "d".
for (var i:int = 0; i < letters.length; i++) {
    trace(letters[i]);
}

Section 5.6: Converting a String to an Array

Problem

You have a list of values as a string and you want to parse it into an array of separate elements.

Solution

Use the String.split( ) method.

Discussion

The split( ) method of the String class splits a string containing a list of values into an array. The list must be delimited by a uniform substring. For example, the list Susan,Robert,Paula is comma-delimited.

The split( ) method takes up to two parameters:

delimiter
The substring that is used to delimit the elements of the list. If undefined, the entire list is placed into the first element of the new array.
limit
The maximum number of elements to place into the new array. If undefined, all the elements of the list are placed into the new array.

You can use a space as the delimiter to split a string into an array of words:

var list:String = "Peter Piper picked a peck of pickled peppers";
// Split the string using the space as the delimiter. This puts 
// each word into an element of the new array, words.
var words:Array = list.split(" ");

The split( ) method can be extremely useful when values are loaded into Flash using a URLLoader object or another similar technique for loading data. For example, you might retrieve a list of names as a string from the server such as the following:

names=Michael,Peter,Linda,Gerome,Catherine

You can make it easier to use the names by parsing them into an array using the split( ) method:

// Assume _loader is the URLLoader used to load the data.
var namesData:String = _loader.data;
var names:Array = namesData.split(",");

See Also

Recipe 5.7

Pages: 1, 2, 3, 4, 5, 6, 7, 8

Next Pagearrow