Contact Us 1-800-596-4880

slice

slice<T>(array: Array<T>, from: Number, until: Number): Array<T>

Selects the interval of elements that satisfy the condition: from <= indexOf(array) < until

Introduced in DataWeave version 2.2.0.

Parameters

Name Description

array

The array of elements.

from

The starting index of the interval of elements to include from the array.
If this value is negative, the function starts including from the first element of the array. If this value is higher than the last index of the array, the function returns an empty array ([]).

until

The ending index of the interval of elements to include from the array.
If this value is higher than the last index of the array, the function includes up to the last element of the array. If this value is lower than the first index of the array, the function returns an empty array ([]).

Example

This example returns an array that contains the values of indices 1, 2, and 3 from the input array. It excludes the values of indices 0, 4, and 5.

Source

%dw 2.0
import * from dw::core::Arrays
output application/json
var arr = [0,1,2,3,4,5]
---
slice(arr, 1, 4)

Output

[
  1,
  2,
  3
]

slice(array: Null, from: Any, until: Any): Null

Helper function that enables slice to work with a null value.

Introduced in DataWeave version 2.4.0.