Array Functions
append
arr.append(elem: any...): arrAdds one or more elements to the end of an array and returns a new array
Arguments:
elem(any) - Any amount of elements to add to the array
Input Example:
{{ ["one", "two"].append("three", "four") }}Output:
one, two, three, fourcontains
arr.contains(elem?: any): boolReturns true if the array contains the given element, otherwise false
Arguments:
elem(any) - The element to search for in the array. Can be any type, including objects and arrays
Input Example:
{{ ["one", "two"].contains("two") }}Output:
1Notes
You can do deep comparison with objects and arrays as well
{{ obj = { name: 'Anna' }; [obj].contains(obj) }}Keep in mind that the order of object's fields doesn't matter in the comparison, but the order of array elements does matter, because each element has a unique index. [1, 2] and [2, 1] are different arrays.
join
arr.join(separator?: str = ","): strJoins the elements of an array into a string and returns it. It takes an optional argument separator which is used to join the elements. If no separator is provided, it defaults to a comma
Arguments:
separator(str) (optional) - What separator to use to join the elements. Default is","(comma)
Input Example:
{{ ["one", "two"].join(" ") }}Output:
one twolen
arr.len(): intReturns the length of an array
Input Example:
{{ [1, 2, 3].len() }}Output:
3prepend
arr.prepend(elem: any...): arrAdds one or more elements to the beginning of an array and returns a new array
Arguments:
elem(any) - Any amount of elements to add to the array
Input Example:
{{ ["three", "four"].prepend("one", "two") }}Output:
one, two, three, fourrand
arr.rand(): anyReturns a random element from the array. The return type depends on the type of elements in the array.
Input Example:
{{ [1, 2, 3].rand() }}Output:
2reverse
arr.reverse(): arrReverses the elements of an array and returns a new array
Input Example:
{{ [1, 2, 3].reverse() }}Output:
3, 2, 1shuffle
arr.shuffle(): arrShuffles the elements of an array and returns a new array
Input Example:
{{ [1, 2, 3, 5].shuffle() }}Output:
<!-- The order of the elements will be random -->
2, 1, 3, 5slice
arr.slice(start: int, end?: int): arrReturns a portion of an array. The start argument is the index at which to begin the slice. The end argument is the index at which to end the slice. If end is not provided, it slices to the end of the array
Arguments:
start(int) - The index at which to begin the sliceend(int) (optional) - The index at which to end the slice
Input Example:
{{ [1, 2, 3, 4, 5].slice(1, 3) }}Output:
2, 3No Negative Arguments
start and end arguments cannot be negative. If you provide a negative value for start, it will be treated as 0. If you provide a negative value for end or the value exceeds the length of the array, it will default to the last index of the array
