slice()

Create an Array from the elements of another Array.

Signature

slice(start: Int, end: Int): Array<T>

Description

The slice() method creates an Array from the calling Array by copying the Array elements from the start index, inclusive, up to the end index, exclusive.

Parameters

Parameter Type Required Description

start

Int

Yes

Zero-based Array element index to start copying, inclusive. A negative start counts from the end of the Array, start + length.

end

Int

Yes

Zero-based Array element index to end copying, exclusive. A negative end counts from the end of the Array, end + length. When end is larger than the Array length, all elements are copied from start to the last Array element.

Return value

Type Description

Array

A new Array with elements from the start index up to the end index of the calling Array

Examples

Create an Array of the second and third elements of the calling Array:

[1, 2, 3, 4, 5].slice(1,3)
[
  2,
  3
]

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!