Handlebar Helper Function: Slice
Returns a portion of a list between two specified indices, similar to JavaScript's Array.slice().
| Parameter | Type | Description |
|---|---|---|
list | array | Required. The list to slice. |
startIndex | number | Required. The starting index. Use negative indices to count from the end. |
endIndex | number | The ending index. Use negative indices to count from the end. If you don’t specify an end index, the function returns a sublist from the startIndex to the end of the list. |
The function returns a sublist from startIndex to endIndex, not including the element at the endIndex. List indices start at 0. If indices are out of bounds or invalid, the function throws an exception. If the array is empty, the function returns an empty list.
This example shows how to use the slice function to return a sublist.
The function returns ["banana", "cherry"].
If you don't specify an end index, the function returns a sublist from the startIndex to the end of the list, as shown in this example.
The function returns ["cherry", "date", "elderberry"].
You can also use negative indices to index the list from the end. Negative indices start at -1. The element at the endIndex isn’t included in the sublist, regardless of whether it’s positive or negative.
This example shows how to use negative indices to index the list from the end.
The function returns ["date"].
You can also specify a positive startIndex and a negative endIndex. This example shows how to return a list that includes the second through the second-to-last elements.
The function returns ["banana", "cherry", "date"].