Skip to main content

array.slice(startIndex, endIndex)

Creates a new sub-array from an array

Availability

Device + Agent

Parameters

Name Type Description
startIndex Integer Index of the first item to be added to the sub-array
endIndex Integer Optional index after the last item to be added to the sub-array

Returns

Array — the requested sub-array

Description

This method copies a series of array items from the target array into a new array which it returns. It takes as its first argument the index of the first item to be copied from the source array. The first item in a Squirrel array is always at index zero.

Squirrel continues copying items from the start index onward. If an end index was passed into the method’s second parameter (this is optional), Squirrel stops copying when it reaches this index. The item with the same index as the second argument is therefore not copied. If the provided end index lies beyond the end of the source array, an exception ("slice out of range") will be thrown.

If no second argument is provided, slice() copies into the new array all of the items up to and including the last item in the source array.

Example Code