Skip to content

Gets a field from a record or an element from a list.

get(x:record, field:string, [fallback:any]) -> any
get(x:record|list, index:int|uint, [fallback:any]) -> any

The get function returns the record field with the name field or the list element with the index index. If fallback is provided, the function gracefully returns the fallback value instead of emitting a warning and returning null.

A record or list you want to access.

A signed or unsigned integer index, or a field to access. If the function’s subject xs is a list, index refers to the position in the list. If the subject xs is a record, index refers to the field index. If the subject is a record, you can also use the field’s name as a string to refer to it.

If the given index or field does not exist in the subject and no fallback was provided, the function raises a warning and returns null.

A fallback value to return if the given index or field do not exist in the subject. Providing a fallback avoids a warning.

Get the first element of a list, or a fallback value

Section titled “Get the first element of a list, or a fallback value”
from (
{xs: [1, 2, 3]},
{xs: []},
}
select first = xs.get(0, -1)
{first: 1}
{first: -1}

Access a field of a record, or a fallback value

Section titled “Access a field of a record, or a fallback value”
from (
{x: 1, y: 2},
{x: 3},
}
select x = this.get("x", -1), y = this.get("y", -1)
{x: 1, y: 2}
{x: 3, y: -1}

Last updated: