Map API

Functions for querying and manipulating maps (key-value dictionaries). Maps use string keys and can hold any value type.

Accessors

size(map)

Returns the number of key-value pairs.

var m = { name: "Alice", age: 30 }
size(m)    // 2
size({})   // 0
isEmpty(map)

Checks whether a map has no entries.

isEmpty({})                    // true
isEmpty({ name: "Alice" })     // false
keys(map)

Returns a list of all keys. Order is not guaranteed.

var m = { name: "Alice", age: 30 }
keys(m)    // ["name", "age"]
values(map)

Returns a list of all values.

values(m)    // ["Alice", 30]
entries(map)

Returns a list of [key, value] pairs.

var m = { x: 1, y: 2 }
entries(m)    // [["x", 1], ["y", 2]]

Mutators

clear(map)

Removes all entries from a map.

var m = { a: 1, b: 2 }
clear(m)     // m is now {}
merge(target, source)

Copies all entries from source into target. Existing keys in target are overwritten by source.

var base = { a: 1, b: 2 }
var extra = { b: 99, c: 3 }
merge(base, extra)
// base is now { a: 1, b: 99, c: 3 }

Shorthand Syntax

When a map key has the same name as the variable you want to assign to it, you can use shorthand syntax — just write the identifier without a colon or value:

var name = "Alice"
var age = 30

// Shorthand
var user = { name, age }

// Equivalent to:
var user = { name: name, age: age }

Shorthand entries can be mixed freely with explicit key-value pairs and spread:

func add(a, b) { return a + b }
func mul(a, b) { return a * b }

var math = { add, mul, version: "1.0" }

This is especially useful for module return values:

// Instead of: return { increment: increment, getCount: getCount }
return { increment, getCount }

Key Deletion

Maps use null-deletion semantics — assigning null to a key removes it:

var m = { name: "Alice", age: 30 }
m.age = null     // key "age" is deleted

See also: List APIString APIConversions API