Map methods

# Michał Wadas (a year ago)

With Set methods being discussed on next TC39 I would like to write second proposal - Map methods.

Map.prototype.filter(cb, thisArg) Map.prototype.mapValues(cb, thisArg) Map.prototype.mapKeys(cb, thisArg) Map.prototype.merge(iterable: Iterable.<Tuple.<*, *>>)

Map.groupBy(iterable, keyDerivativeFunc) - analogus to lodash.groupBy lodash.com/docs/4.17.4#groupBy, but returning Map Instance.

Map.keyBy(iterable, keyDerivativeFunc) - analogous to lodash.keyBy, but returning Map instance.

I spend few minutes on writing code for simple polyfill - Ginden/map-methods/blob/master/polyfill.js and I'm gathering opinions what would be expected final shape of Map API.

# Cyril Auburtin (10 months ago)

I totally agree that Map (and Set) should have `.map`, `.filter` and a few more (`.slice`, ..) methods, similarly to immutableJS. Since Maps and Sets are ordered, it would make sense

example polyfill:

``````class SortableMap {
constructor(data) {
this.m = new Map(Array.isArray(data) ? data : Object.entries(data));
}
set(k, v) {
this.m.set(k, v);
return this;
}
get(k) {
return this.m.get(k);
}
forEach(fn) {
this.m.forEach(fn);
}
map(fn) {
return Array.from(this.m, fn);
}
sort(fn) {
const a = [...this.m].sort((x, y) => fn(x[1], y[1])); // sort with values

this.m = new SortableMap(a);
return this;
}
*[Symbol.iterator]() {
yield* this.m;
}
}
const m = new SortableMap([["lol", "far"], ["ok", "boo"]]);
m.sort((a, b) => a.localeCompare(b));
console.log(1, m);
// 1 SortableMap { m: SortableMap { m: Map { 'ok' => 'boo', 'lol' => 'far'
m.set("bip", "cri");
console.log(2, m.map());
// 2 [ [ 'ok', 'boo' ], [ 'lol', 'far' ], [ 'bip', 'cri' ] ]
m.sort((a, b) => a.localeCompare(b));
console.log(3, m.map(([k, v]) => [k, v + "!"]));
// 3 [ [ 'ok', 'boo!' ], [ 'bip', 'cri!' ], [ 'lol', 'far!' ] ]
``````