Skip to main content
Version: v2

Array Functions

contains

contains(elem?: any): bool

Returns true if the array contains the given element, otherwise false

Arguments

  1. 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:

1

Notes

You can do deep comparison with objects and arrays as well

{{ obj = { name: 'Anna' }; [obj].contains(obj) }}

Keep in mind that the order of objects 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

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

  1. separator (str) (optional) - What separator to use to join the elements. Default is "," (comma)

Input example:

{{ ["one", "two"].join(" ") }}

Output:

one two

len

len(): int

Returns the length of an array

Input example:

{{ [1, 2, 3].len() }}

Output:

3

rand

rand(): any

Returns 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:

2

reverse

reverse(): arr

Reverses the elements of an array and returns a new array

Input example:

{{ [1, 2, 3].reverse() }}

Output:

3, 2, 1

shuffle

shuffle(): arr

Shuffles 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, 5

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

  1. start (int) - The index at which to begin the slice
  2. end (int) (optional) - The index at which to end the slice

Input example:

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

Output:

2, 3
No 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 will exceed the length of the array, it will default to a value of the last index of the array