Maps
Maps (dictionaries) are unordered key-value hash tables. Keys can be any type.
Creating Maps
let empty = {}
let user = {"name": "Riad", "age": 30, "active": True}
print(user["name"]) # => Riad
Map Methods
| Method | Description |
|---|---|
length() | Number of entries |
keys() | Array of keys |
values() | Array of values |
items() | Array of [key, value] pairs |
has(key) | Check if key exists |
get(key) | Get value (Null if missing) |
copy() | Shallow copy of map |
clear() | Remove all entries |
pop(key) | Remove key and return value |
setDefault(k, v) | Set only if key missing |
update(other) | Merge entries from another map |
Examples
let m = {"a": 1, "b": 2}
print(m.length()) # => 2
print(m.has("a")) # => True
print(m.keys()) # => ["a", "b"]
print(m.values()) # => [1, 2]
print(m.get("a")) # => 1
print(m.get("x")) # => Null
m.setDefault("c", 3) # adds {"c": 3}
print(m.pop("a")) # => 1 (removes "a")
m.update({"d": 4}) # adds {"d": 4}
Iteration
for key, value in {"name": "John", "age": 30}
print("${key}: ${value}")
end