Creates a Map
object that contains all key/ value pairs of mapEntries
and an optional mapFn
to call on every element of the mapEntries
.
const entries = [
[1, 1],
[2, 2],
];
const m = Map.from(entries, (n) => {
const k = n[0];
const v = n[1];
return [k, 1 + v];
});
m.get(1) === 2;
m.get(2) === 3;
Returns true
if the x
is a Map
object.
Map.isMap(new Map()) === true;
Creates a new Map
object from a variable number of arguments, regardless of the number of type of the arguments.
const m = Map.of([1, 1], [2, 2]);
m.get(1) === 1;
m.get(2) === 2;
Gets the given key
's corresponding entry in the map, or [key, undefined]
if this map contains no mapping for the key.
const m = new Map();
m.set(1, 1);
m.set(2, 2);
m.entry(1); /** [1, 1] */
m.entry(3); /** [3, undefined] */
Gets the given key
's corresponding entry in the map, or [key, defaultValue]
if this map contains no mapping for the key.
const m = new Map();
m.set(1, 1);
m.set(2, 2);
m.entry(1); /** [1, 1] */
Returns the value to which the specified key is mapped, or defaultValue
if this map contains no mapping for the key.
const m = new Map();
'hello world'.split('').filter(Boolean).forEach((n) => {
const count = 1 + m.getOrDefault(n, 0);
m.set(n, key);
});
m.get('o') === 2;
m.get('l') === 3;
Returns true
if the map contains no elements.
const m = new Map();
m.isEmpty() === true;
m.set(1, 1);
m.set(2, 2);
m.isEmpty() === false;
Returns an iterator visiting all key-value pairs.
const m = new Map();
m.set(1, 1);
m.set(2, 2);
const iter = m.iter();
iter.next().value; /** [1, 1] */
iter.next().value; /** [2, 2] */
iter.next().done() === true;
Returns the number of elements in the map.
const m = new Map();
m.len() === 0;
m.set(1, 1);
m.set(2, 2);
m.len() === 2;
Removes the given key
from the map, returning the stored key and the value if the key was previously in the map.
const m = new Map();
m.set(1, 1);
m.set(2, 2);
m.removeEntry(1); /** [1, 1] */
m.has(1) === false;
Creates an array containing all the elements of the map.
const m = new Map();
m.set(1, 1);
m.set(2, 2);
m.toArray(); /** [[1, 1], [2, 2]] */