A Singapore Government Agency Website

Array

Array is a collection that holds multiple values of various types, such as numbers, strings, or booleans. It is defined using square brackets [], with elements listed inside.

['a', 'b', 'c'] // string array
[1, 2, 3]       // number array
[true, false]   // boolean array

Operators


Access operator

You can retrieve values from arrays using dot notation (.key) or bracket notation ([key]).

// Suppose customer.groups = ["admin", "user"]
customer.groups[0]    // "admin"
customer.groups[1]    // "user"

Membership operators

  • Element of (in): a in array
  • Not element of (not in): a not in array
1 in [1, 2, 3]       // true
5 not in [1, 2, 3]   // true

Range operators

Shorthand symbols for comparing values using greater than or less than logic.

Expression Equivalent
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

Functions


len

Returns the total number of elements in the array.

len([1, 2, 3]); // 3

sum

Returns the sum of all values in a number array.

sum([1, 2, 3]); // 6

avg

Returns the average value of a numeric array.

avg([1, 2, 3]); // 2

min

Returns the smallest number within the number array.

min([1, 2, 3]); // 1

max

Returns the largest number in the number array.

max([1, 2, 3]); // 3

median

Returns the median (middle value) of a numeric array.

median([1, 2, 3, 4, 5]); // 3
median([1, 2, 3, 4]);    // 2.5

mode

Returns the most frequently occurring number in the array.

mode([1, 1, 2, 2, 2, 3, 5, 8]); // 2

contains

Checks if the specified value exists in the array; returns true if found.

contains(['a', 'b', 'c'], 'a'); // true
contains([1, 2, 3], 5); // false

flatten

Reduces nested arrays by one level of depth.

flatten([1, 'a', ['b', 'c'], [4]]); // [1, "a", "b", "c", 4]
flatten([
  [1, 2, 3],
  [4, 5, 6],
]); // [1, 2, 3, 4, 5, 6]

keys

Returns an array of keys from an object.

// Given input = { "name": "John", "age": 30 }
keys(input); // ["name", "age"]

values

Returns an array of values from an object.

// Given input = { "name": "John", "age": 30 }
values(input); // ["John", 30]

Closure Functions


Closure functions allow you to pass a condition or transformation as an argument. These iterate through the array, with the special variable # representing the current item.

You can also use the as keyword to give a named alias to the current item, which is useful for readability or when working with nested closures:

filter(orders, # as order; order.total > 100)

all

Returns true if every item in the array meets the specified condition.

all(["a", "b"], # == "a")   // false
all([1, 2, 3], # in [1..3]) // true

some

Returns true if at least one item satisfies the given condition.

some(["a", "b"], # == "a") // true
some([1, 2, 3], # > 5)     // false

one

Returns true if exactly one item satisfies the condition.

one([1, 2, 3], # == 2)    // true
one([1, 2, 2], # == 2)    // false

none

Returns true if no item meets the condition.

none(["a", "b"], # == "a") // false
none([1, 2, 3], # > 5)     // true

filter

Produces a new array with only the items that match the condition.

filter([1, 2, 3, 4], # > 1) // [2, 3, 4]

map

Transforms each item in the array and returns a new array with the updated values.

map(["world", "user"], "hello " + #) // ["hello world", "hello user"]
map([1, 2, 3], # + 5)                // [6, 7, 8]

flatMap

Maps and then flattens the resulting array by one level.

flatMap([[1, 2, 3], [4, 5, 6]], map(#, # + 1)) // [2, 3, 4, 5, 6, 7]

count

Similar to len, but only counts items that match a specified condition.

count([1, 2, 3], # > 1) // 2
Chat with me!
Bot Launcher