Section 24.14. Array.slice( ): return a portion of an array


24.14. Array.slice( ): return a portion of an array

ECMAScript v3

24.14.1. Synopsis

array.slice(start, end)

24.14.1.1. Arguments

start

The array index at which the slice is to begin. If negative, this argument specifies a position measured from the end of the array. That is, -1 indicates the last element, -2 indicates the next from the last element, and so on.


end

The array index immediately after the end of the slice. If not specified, the slice includes all array elements from the start to the end of the array. If this argument is negative, it specifies an array element measured from the end of the array.

24.14.1.2. Returns

A new array that contains the elements of array from the element specified by start, up to, but not including, the element specified by end.

24.14.2. Description

slice( ) returns a slice, or subarray, of array. The returned array contains the element specified by start and all subsequent elements up to, but not including, the element specified by end. If end is not specified, the returned array contains all elements from the start to the end of array.

Note that slice( ) does not modify the array. If you want to actually remove a slice of an array, use Array.splice( ).

24.14.3. Example

 var a = [1,2,3,4,5]; a.slice(0,3);    // Returns [1,2,3] a.slice(3);      // Returns [4,5] a.slice(1,-1);   // Returns [2,3,4] a.slice(-3,-2);  // Returns [3]; buggy in IE 4: returns [1,2,3] 

24.14.4. Bugs

start can't be a negative number in Internet Explorer 4. This is fixed in later versions of IE.

24.14.5. See Also

 Array.splice( ) 




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

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