Name
Array.slice( ) Method — create a new array using a subset of elements from an existing array
Availability
Flash 5
Synopsis
array.slice(startIndex, endIndex)
Arguments
- startIndex
A zero-relative integer specifying the first element in
arrayto add to the new array. If negative,startIndexindicates an element number counting backward from the end ofarray(-1 is the last element, -2 is the second-to-last element, etc.).
- endIndex
An integer specifying the element after the last element in
arrayto add to the new array. If negative,endIndexcounts backward from the end ofarray(-1 is the last element, -2 is the second-to-last element, etc.). If omitted,endIndexdefaults toarray.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 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( ); Section 11.9.3 in Chapter 11, Section 5.11.4 in Chapter 5
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access