Map

map~ Map

new Map()

Creates a new instance of a Map-like object compatible with older JavaScript environments
such as Rhino or RingoJS that do not support ES6 Map.

Internally stores keys and values using an array and plain object, preserving insertion order.
Provides chainable methods and supports functional-style transformations like map, filter, reduce.

Example
var { Map } = require("Map");
var m = new Map();
m.set("foo", 123).set("bar", 456);
print(m.get("foo"));  // → 123
print(m.has("bar"));  // → true
m.forEach((v, k) => print(k + ": " + v)); // → foo: 123\nbar: 456

clear()

Clear all entries.

Example
var m = new Map();
m.set("a", 1).set("b", 2);
m.clear();
print(m.size()); // 0

clone() → {Map}

Clone the map (shallow copy).

Example
var m1 = new Map();
m1.set("a", 1);
var m2 = m1.clone();
print(m2.get("a")); // 1
Returns:
Map

delete(key) → {boolean}

Delete a key.

Example
var m = new Map();
m.set("p", 42);
print(m.delete("p")); // true
print(m.delete("q")); // false
Parameters:
* key
Returns:
boolean

true if deleted, false if not found

entries() → {Array.<Array>}

Get key-value pairs.

Example
var m = new Map();
m.set("a", 1).set("b", 2);
var e = m.entries(); // [["a",1], ["b",2]]
Returns:
Array.<Array>

Array of [key, value] pairs

filter(predicate) → {Map}

Filter entries.

Example
var m = new Map();
m.set("a", 1).set("b", 5);
var gt2 = m.filter(v => v > 2);
print(gt2.keys()); // ["b"]
Parameters:
function predicate

fn(value, key, map) returning true to keep

Returns:
Map

New filtered map

forEach(callback, thisArgopt)

Loop over all entries.

Example
var m = new Map();
m.set("a", 1).set("b", 2);
m.forEach((v, k) => print(k + ":" + v));
Parameters:
function callback

callback(value, key, map)

* thisArg <optional>

Optional context for callback

get(key) → {*}

Get value by key.

Example
var m = new Map();
m.set("x", 123);
print(m.get("x")); // 123
print(m.get("y")); // undefined
Parameters:
* key
Returns:
*

value or undefined if not found

has(key) → {boolean}

Check if key exists.

Example
var m = new Map();
m.set("z", 9);
print(m.has("z")); // true
print(m.has("y")); // false
Parameters:
* key
Returns:
boolean

isEmpty() → {boolean}

Check if map is empty.

Example
var m = new Map();
print(m.isEmpty()); // true
m.set("a", 5);
print(m.isEmpty()); // false
Returns:
boolean

keys() → {Array}

Get all keys.

Example
var m = new Map();
m.set("a", 1).set("b", 2);
var k = m.keys(); // ["a", "b"]
Returns:
Array

map(transformFn) → {Map}

Transform values.

Example
var m = new Map();
m.set("x", 2);
var m2 = m.map(v => v * 10);
print(m2.get("x")); // 20
Parameters:
function transformFn

fn(value, key, map)

Returns:
Map

New map with transformed values

merge(otherMap) → {Map}

Merge with another map (right-hand map overwrites on conflict).

Example
var a = new Map().set("x", 1);
var b = new Map().set("x", 2).set("y", 3);
var merged = a.merge(b);
print(merged.get("x")); // 2
print(merged.get("y")); // 3
Parameters:
Map otherMap
Returns:
Map

New merged map

reduce(reducer, initialValue) → {*}

Reduce entries.

Example
var m = new Map();
m.set("a", 1).set("b", 4);
var sum = m.reduce((acc, v) => acc + v, 0);
print(sum); // 5
Parameters:
function reducer

fn(accumulator, value, key, map)

* initialValue

starting accumulator value

Returns:
*

Final accumulated result

reverse() → {Map}

Reverse key order in-place.

Example
var m = new Map();
m.set("a", 1).set("b", 2);
m.reverse();
print(m.keys()); // ["b", "a"]
Returns:
Map

this

set(key, value) → {Map}

Add or update a key-value pair.

Example
var m = new Map();
m.set("a", 1).set("b", 2); // chainable
print(m.get("a")); // 1
m.set("a", 99); // overwrite
print(m.get("a")); // 99
Parameters:
* key

Key to insert or update

* value

Value to associate with the key

Returns:
Map

this (chainable)

size() → {number}

Get number of keys.

Example
var m = new Map();
m.set("x", 1).set("y", 2);
print(m.size()); // 2
Returns:
number

sortKeys(compareFnopt)

Sort by keys.

Example
var m = new Map();
m.set("b", 2).set("a", 1);
m.sortKeys();
print(m.keys()); // ["a", "b"]
Parameters:
function compareFn <optional>

Optional comparator for keys

sortValues(compareFn)

Sort by values.

Example
var m = new Map();
m.set("a", 2).set("b", 1);
m.sortValues((va, vb) => va - vb);
print(m.values()); // [1, 2]
Parameters:
function compareFn

Comparator for values

toString() → {string}

Get string representation of map.

Example
var m = new Map();
m.set("a", 1).set("b", 2);
print(m.toString()); // "{a=1, b=2}"
Returns:
string

values() → {Array}

Get all values.

Example
var m = new Map();
m.set("a", 1).set("b", 2);
var v = m.values(); // [1, 2]
Returns:
Array