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
Methods
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:
- Type
- 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:
| Name | Type | Description |
|---|---|---|
key |
* |
Returns:
true if deleted, false if not found
- Type
- boolean
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 of [key, value] pairs
- Type
- Array.<Array>
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:
| Name | Type | Description |
|---|---|---|
predicate |
function | fn(value, key, map) returning true to keep |
Returns:
New filtered map
- Type
- 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:
| Name | Type | Attributes | Description |
|---|---|---|---|
callback |
function | 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:
| Name | Type | Description |
|---|---|---|
key |
* |
Returns:
value or undefined if not found
- Type
- *
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:
| Name | Type | Description |
|---|---|---|
key |
* |
Returns:
- Type
- 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:
- Type
- 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:
- Type
- 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:
| Name | Type | Description |
|---|---|---|
transformFn |
function | fn(value, key, map) |
Returns:
New map with transformed values
- Type
- Map
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:
| Name | Type | Description |
|---|---|---|
otherMap |
Map |
Returns:
New merged map
- Type
- 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:
| Name | Type | Description |
|---|---|---|
reducer |
function | fn(accumulator, value, key, map) |
initialValue |
* | starting accumulator value |
Returns:
Final accumulated result
- Type
- *
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:
this
- Type
- Map
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:
| Name | Type | Description |
|---|---|---|
key |
* | Key to insert or update |
value |
* | Value to associate with the key |
Returns:
this (chainable)
- Type
- Map
size() → {number}
Get number of keys.
Example
var m = new Map();
m.set("x", 1).set("y", 2);
print(m.size()); // 2
Returns:
- Type
- 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:
| Name | Type | Attributes | Description |
|---|---|---|---|
compareFn |
function |
<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:
| Name | Type | Description |
|---|---|---|
compareFn |
function | 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:
- Type
- 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:
- Type
- Array