Array
Array represents a list of other data types (list of numbers, strings, booleans). It's declared by opening a square bracket, writing a list of desired data types, and closing it.
['a', 'b', 'c'] // string array
[1, 2, 3] // number array
[true, false] // boolean array
Operators
Access operator
Properties in the array can be accessed using dot syntax .key
or [key]
.
For example:
// Suppose customer.groups = ["admin", "user"]
customer.groups
.0 // "admin"
customer.groups[1] // "user
Membership operators
Operator | Description | Example |
---|---|---|
in | Element of | a in array |
not in | Not element of | a not in array |
Range operators
Useful shorthand operators for greater than and less than.
Operator example | Complete form |
---|---|
Closed interval | |
x in [a..b] | x >= a and x <= b |
x in (a..b) | x > a and x < b |
x in [a..b) | x >= a and x < b |
x in (a..b] | x > a and x <= b |
Open interval | |
x in ]a..b[ | x <= a or x >= b |
x in )a..b( | x < a or x > b |
x in ]a..b( | x <= a or x = b |
x in )a..b[ | x < a or x >= b |
Functions
len
Accepts an array and returns its length.
Syntax:
len([1, 2, 3]); // 3
sum
Accepts a number array and returns the sum of all elements.
Syntax:
sum([1, 2, 3]); // 6
avg
Accepts a number array and returns the average of all elements.
Syntax:
avg([1, 2, 3]); // 2
min
Accepts a number array and returns the smallest element.
Syntax:
min([1, 2, 3]); // 1
max
Accepts a number array and returns the largest element.
Syntax
max([1, 2, 3]); // 3
mean
Accepts a number array and returns the mean.
Syntax
mean([1, 2, 3]); // 2
mean([1, 2, 3, 4]); // 2.5
mode
Accepts a number array and returns the mode (most common occurrence).
Syntax
mode([1, 1, 2, 2, 2, 5, 6, 9]); // 2
contains
Accepts an array and a search parameter. Returns true if element exists within the array.
Syntax
contains(['a', 'b', 'c'], 'a'); // true
contains([1, 2, 3], 5); // false
flatten
Accepts and array and flattens parameters by a single level.
Syntax
flatten([1, 'a', ['b', 'c'], [4]]); // [1, "a", "b", "c", 4]
flatten([
[1, 2, 3],
[4, 5, 6],
]); // [1, 2, 3, 4, 5, 6]
Closure functions
Closure functions allow you to define a callback as a special argument, which iterates over all elements
of the array. Within the closure, you have access to current element by using the #
variable.
all
Returns true if all elements of the array satisfy the condition.
all(["a", "b"], # == "a") // false
all([1, 2, 3], # in [1..3]) // true
some
Returns true if at least one of the elements in the array satisfies the condition.
Syntax
some(["a", "b"], # == "a") // true
some([1, 2, 3], # > 5) // false
none
Returns true if no element in array satisfies the condition.
Syntax
none(["a", "b"], # == "a") // false
none([1, 2, 3], # > 5) // true
filter
Returns a new array which contains only the elements that satisfy the condition.
Syntax
filter([1, 2, 3, 4], # > 1) // [2, 3, 4]
map
Returns a new array with re-mapped values.
Syntax
map(["world", "user"], "hello " + #) // ["hello world", "hello user"]
map([1, 2, 3], # + 5) // [6, 7, 8]
flatMap
Returns a new array with re-mapped values and flattens them.
flatMap([[1, 2, 3], [4, 5, 6]], map(#, # + 1)) // [2, 3, 4, 5, 6, 7]
count
Similar to len
, but with filter
applied.
Syntax
count([1, 2, 3], # > 1) // 2