Skip to main content
When you have an array of keys and an array of the corresponding values, build a Map from them with mapFromArrays, then use arrayMap to replace every key of another array with its value. For nested arrays of keys, nest arrayMap.

Example

The array k holds the keys, v holds the values at the same positions, and r holds nested arrays of keys to translate:
How it works:
  1. mapFromArrays(k, v) pairs the elements of the two arrays by position and returns a Map. Both arrays must have the same size.
  2. The outer arrayMap processes each inner array i of r.
  3. The inner arrayMap processes each key ii of the inner array and replaces it with m[ii], the value stored in the map for that key.
For a flat array of keys, one arrayMap is enough: arrayMap(x -> m[x], keys_to_translate).

Use it with table columns

The arrays can be columns, so every row can have its own mapping:

Keys that are missing from the map

If a key is not in the map, m[key] returns the default value of the value type: an empty string for String, 0 for numbers. Use mapContains to return NULL instead, or to skip such keys:

Constant mappings

If the keys and values are constant arrays, the transform function does the same lookup without a map and lets you choose the value for missing keys:
transform requires its second and third arguments to be constants, so it does not work when the mapping comes from table columns.

Things to keep in mind

  • m[key] scans the map, so the time of one lookup grows linearly with the size of the map. This is fine for small mappings stored in each row. For a large mapping shared by all rows, use a dictionary and dictGet instead.
  • A Map can contain the same key more than once. If the keys array has duplicates, decide which value you want before building the map.
See the Map(K, V) data type for details.