Skip to main content

Map type

A map contains multiple values, each of which can be looked up by a key stored alongside it. In other words: keys are mapped to values, which is where the name "map" comes from.

Values can be of any type, so long as they all have the same type. Keys are currently limited to either Number or Text:

  • a map with number keys: [ 15 : "value" ]
  • a map with text keys: [ "one" : 1, "two" : 2 ]
info

The items of a map are ordered and can also be accessed by their index / position in the map, in addition to their key.
The first item has the index 1.

Input

A map can also be used as input for a rule. It works the same for them as for any other value:

Input or [ "one" : 1 ]

In this example, if input contains a map, that map is used – otherwise the map [ "one" : 1 ] is used.

Empty Maps ([])

You can create a map without any items in it with a pair of square brackets ([]) – the same syntax used for empty lists.

info

Empty maps only work in places where the type of the map can be inferred!

Input or []

MapEntry

When you access an item of a map by its index (e.g. with getEntryAt), you get back a MapEntry – a value that holds both the key and the value of that item: { key: …, value: … }.

Functions

namedescription
lengthgets the number of items in the map
get(key)gets the value for the specified key
getAt(index)gets the value at the specified index
getEntryAt(index)gets the key and value at the specified index

length

Returns the number of items in the map.

ExampleResult
[ "one" : 1, "two" : 2 ].length()2

get(key)

Returns the value for the specified key, or Empty if the map contains no such key.

ExampleResult
[ "one" : 1, "two" : 2 ].get("one")1
[ "one" : 1, "two" : 2 ].get("three")Empty

When looking for a key, get uses the default equality operator as defined by the type of the keys in the map. Text keys are compared case-sensitive.


getAt(index)

Returns the value at the specified index.

ExampleResult
[ "one" : 1, "two" : 2 ].getAt(0)1 (same as getAt(1))
[ "one" : 1, "two" : 2 ].getAt(2)2
[ "one" : 1, "two" : 2 ].getAt(-1)2

getAt behaves the same way as List.get(index):

  • The first item has index 1, but 0 also returns it.
  • When the index is negative, items are counted from right to left.
  • When the index is greater than the number of items in the map, it is automatically capped.
  • If the map contains no items, Empty is returned. Check the length of the map to explicitly handle this case.

getEntryAt(index)

Returns the MapEntry (key and value) at the specified index. Use this when you need the key of an item you only know by its index. Indexing rules are the same as for getAt.

ExampleResult
[ "one" : 1, "two" : 2 ].getEntryAt(0){ key: "one", value: 1 }