Reference · Maps
keys
signature
keys(map | null) -> listWhat it does
The keys of a map, as a list of strings, in the order the entries were added. A map's
keys are always text, so a key that was set with a number comes back as its text.
null counts as an empty map and gives [].
Examples
let user = { name: "Ann", age: 41, admin: false }print(keys(user))print(keys({ "x y": 1, "2": 2 }))output
["name", "age", "admin"]["x y", "2"]Edge cases
- Order is insertion order, not sorted: sort the list when an order is wanted. It is
the same in the browser, keys that look like numbers included:
keys({ b: 1, "1": 2 })is["b", "1"]on both sides, where a JavaScript object lists"1"first. seton a key that is already there keeps its place;removeandsetagain puts it at the end.- An empty map and
nullboth give[]. len(keys(m))islen(m).
let m = { b: 2, a: 1, c: 3 }print(keys(m), keys(m) | sort)print(keys(set(m, "a", 10)), keys(set(remove(m, "a"), "a", 10)))print(keys({}), keys(null))print(len(keys(m)), len(m))print(keys({ b: 1, "1": 2, a: 3, "0": 4 }))output
["b", "a", "c"] ["a", "b", "c"]["b", "a", "c"] ["b", "c", "a"][] []3 3["b", "1", "a", "0"]Errors
A list, a string or a number is a runtime error, not an error value: keys takes a
map or null only. A list has no keys; enumerate gives its positions.
this one fails
print(keys([1, 2]))output
line 1: `keys` expects a map as argument 1, got listChaining
In a pipeline the map comes first. There is no entries: for [k, v] in m walks a map
directly.
print({ b: 1, a: 2 } | keys | map(upper))for [k, v] in { a: 1, b: 2 } { print(k, v) }output
["B", "A"]a 1b 2See also
Thread
Thread 0 posts
On keys(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo