Bagikan melalui


slice Method (Array)

Returns a section of an array.

function slice(start : Number [, end : Number]) : Array

Arguments

  • start
    Required. The index to the beginning of the specified portion of the array.

  • end
    Optional. The index to the end of the specified portion of the array.

Remarks

The slice method returns an Array object containing the specified portion of the array.

The slice method copies up to, but not including, the element indicated by end. If start is negative, it is treated as length + start where length is the length of the array. If end is negative, it is treated as length + end where length is the length of the array. If end is omitted, extraction continues to the end of the array. If end occurs before start, no elements are copied to the new array.

Example

The following example illustrates the use of the slice method.

var myArray = new Array(4,3,5,65);

// Copy all but the last element of myArray
// into newArray1.
var newArray1 = myArray.slice(0, -1)

// Copy only the last two elements of MyArray
// into newArray2.
var newArray2 = myArray.slice(-2)

Requirements

Version 3

Applies To:

Array Object

See Also

Reference

slice Method (String)

String Object

Change History

Date

History

Reason

March 2009

Added a second example.

Information enhancement.