Home

Awesome

New collections (Set and Map) methods

See formal spec WIP.

(Semi)relevant previous discussions

Motivations

Adoption

TBA

Quick reference for other languages similar data structures

Set

Map (known also as Dictionary)

TBA

Proposal

This proposal does not change grammar of language.

New methods are added to Set.prototype.

New methods are added to Map.prototype.

New method is added to %Map%:

New methods are added to WeakSet.prototype.

New method is added to WeakMap.prototype:

Polyfill

A polyfill is available in the core-js library. You can find it in the ECMAScript proposal section / Stage 1 proposals / New Set and Map methods proposal.

For all new collection methods include this at the top of your entry point.

require('core-js/proposals/collection-methods');

const colors = new Map([['red', '#FF0000'], ['gold', '#FFD700']]);
colors
  .mapKeys((value, key) => key.toUpperCase()) // Map { 'RED' => '#FF0000', 'GOLD' => '#FFD700' }
  .mapValues(value => value.toLowerCase()); // Map { 'RED' => '#ff0000', 'GOLD' => '#ffd700' }

For a specific method include the needed polyfill directly.

require('core-js/features/set/join');

const mySet = new Set(['Just', 'like', 'an', 'array']);
mySet.join(' '); // Just like an array

Not included in this proposal but worth considering

Why not %IteratorPrototype% methods

See stage 3 Iterator Helpers proposal.

Rationales

See rationales.md for details.