Maps are a collection of zero or more key/value pairs. These are useful
for storing values that are looked up by a unique key.
Maps can be created using {} and specifying key/value pairs. Keys and
values can be differing types. An optional trailing comma is allowed.
Examples:
// Empty map
{}
// Map with a single value on one line
{ "key": "value" }
// Map with multiple values with differing types on multiple lines
{
"key": "value",
42: true,
}
// Empty map
{}
// Map with a single value on one line
{ "key": "value" }
// Map with multiple values with differing types on multiple lines
{
"key": "value",
42: true,
}
Maps are unordered. When
looping
over a map, the key/value pairs can be returned in any order.
The set operators can be used
to test for key inclusion in a map.
Elements can be added or modified in a map by assigning to name[key].
If the key doesn't exist, the value is added. If the key already exists,
the value is overridden.
map = { "key": "value" }
map[42] = true // Add a new key/value
map["key"] = 12 // Modify the value of "key"
map = { "key": "value" }
map[42] = true // Add a new key/value
map["key"] = 12 // Modify the value of "key"
The keys and values of a map can be retrieved as
lists using the
keys and
values functions.
Because maps are unordered, the keys and values are returned in an
unspecified order. It should not be assumed that keys and values will be
returned in a consistent order.
Maps may be compared for
equality. Maps are equal if they are of equal length and both their
corresponding keys and values are comparable and equal.
{"foo": "bar"} is {"foo": "bar"} // true
{"foo": "bar"} is {"baz": "bar"} // false
{"foo": "bar"} is {"foo": "baz"} // false
{"foo": "bar"} is {"foo": "bar", "baz": "qux"} // false
{1: "a"} is {1.0: "a"} // true (int/float comparable)
// also true (maps are not ordered):
{"m": {"a": "b"}, "l": ["a"]} is {"l": ["a"], "m": {"a": " b"}}
{"foo": "bar"} is {"foo": "bar"} // true
{"foo": "bar"} is {"baz": "bar"} // false
{"foo": "bar"} is {"foo": "baz"} // false
{"foo": "bar"} is {"foo": "bar", "baz": "qux"} // false
{1: "a"} is {1.0: "a"} // true (int/float comparable)
// also true (maps are not ordered):
{"m": {"a": "b"}, "l": ["a"]} is {"l": ["a"], "m": {"a": " b"}}
The current worst-case comparison speed for maps is O(N²), or quadratic
time. This is the square of the cumulative elements or the parent and all child
maps contained within the map. Consider this when making comparisons on larger,
more complex maps. This may be optimized in the future.