Table of Contents

Array.splice( ) Method Flash 5

remove elements from, and/or add elements to, an array
array.splice(startIndex)
array.splice(startIndex,
deleteCount)
array.splice(startIndex, deleteCount, value1,...valuen)

Arguments

startIndex

The zero-relative element index at which to start element deletion and optional insertion of new elements. If negative, startIndex specifies an element counting back from the end of array (-1 is the last element, -2 is the second-to-last element, etc.).

deleteCount

An optional, nonnegative integer representing the number of elements to remove from array, including the element at startIndex. If deleteCount is 0, no elements are deleted. If deleteCount is omitted, all elements from startIndex to the end of the array are removed.

value1, ...valuen

An optional list of one or more values to be added to array at index startIndex after the specified elements have been deleted. If this list is omitted, no elements are added.

Returns

A new array containing the deleted elements (the original array is modified separately to reflect the requested changes).

Description

The splice( ) method removes the elements from array[startIndex] to array[startIndex + deleteCount - 1] and then, optionally, inserts new elements starting at startIndex. The splice( ) method does not leaves gaps in array; it compacts deleted elements and moves remaining elements as needed to ensure the contiguity of elements in the array.

Example

myList = new Array (1, 2, 3, 4, 5);
// Deletes the second and third elements from the list
// and inserts the elements "x", "y", and "z" in their place.
// This changes myList to [1, "x", "y", "z", 4, 5].
myList.splice(1, 2, "x", "y", "z");

See Also

Array.slice( ); "The splice( ) Method," in Chapter 11


Table of Contents