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 arrayk holds the keys, v holds the values at the same positions, and r holds nested arrays of keys to translate:
mapFromArrays(k, v)pairs the elements of the two arrays by position and returns aMap. Both arrays must have the same size.- The outer
arrayMapprocesses each inner arrayiofr. - The inner
arrayMapprocesses each keyiiof the inner array and replaces it withm[ii], the value stored in the map for that key.
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, thetransform 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 anddictGetinstead.- A
Mapcan contain the same key more than once. If the keys array has duplicates, decide which value you want before building the map.
Map(K, V) data type for details.