Table of Contents

Array.slice( ) Method Flash 5

create a new array using a subset of elements from an existing array
array.slice(startIndex, endIndex)

Arguments

startIndex

A zero-relative integer specifying the first element of array to add to the new array. If negative, startIndex indicates an element number counting backward from the end of array (-1 is the last element, -2 is the second-to-last element, etc.).

endIndex

An integer specifying the element after the last element of array to add to the new array. If negative, endIndex counts backward from the end of array (-1 is the last element, -2 is the second-to-last element, etc.). If omitted, endIndex defaults to array.length.

Returns

A new array containing the elements of array from startIndex to endIndex-1.

Description

The slice( ) method creates a new array by extracting a series of elements from an existing array. The original array is not modified. The new array is a subset of the elements of the original array, starting with array[startIndex] and ending with array[endIndex - 1].

Example

myList = new Array("a", "b", "c", "d", "e");
   
// Set myOtherList to ["b", "c", "d"]
myOtherList = myList.slice(1, 4);
   
// Set anotherList to ["d", "e"]
anotherList = myList.slice(3);
   
// Set yetAnotherList to ["c", "d"]
yetAnotherList = myList.slice(-3, -1);

See Also

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


Table of Contents