Array Functions
join
join(separator?: str = ","): str
Joins 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:
<span>{{ ["one", "two"].join(" ") }}</span>
Output:
<span>one two</span>
len
len(): int
Returns the length of an array
Input example:
<span>{{ [1, 2, 3].len() }}</span>
Output:
<span>3</span>
rand
<T>.rand(): T
Returns a random element from the array. The return type is the same as the type of the elements in the array
Input example:
<span>{{ [1, 2, 3].rand() }}</span>
Output:
<span>2</span>
reverse
reverse(): arr
Reverses the elements of an array and returns a new array
Input example:
<span>{{ [1, 2, 3].reverse() }}</span>
Output:
<span>3, 2, 1</span>
shuffle
shuffle(): arr
Shuffles the elements of an array and returns a new array
Input example:
<span>{{ [1, 2, 3, 5].shuffle() }}</span>
Output:
<!-- The order of the elements will be random -->
<span>2, 1, 3, 5</span>
slice
slice(start: int, end?: int): arr
Returns 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:
<span>{{ [1, 2, 3, 4, 5].slice(1, 3) }}</span>
Output:
<span>2, 3</span>
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 will exceed the length of the array, it will default to a value of the last index of the array