JavaScript Collections
ECMAScript2015(ES6) introduced four new collections Map, Set, WeakMap and WeakSet.
Map
A Map object is a simple key/value map and can iterate its elements
in insertion order.
let barMap = new Map();
barMap.set('one', 1);
barMap.set('two', 2);
barMap.set('three', 3);
for (let [key, value] of barMap) {
console.log(key + ' = ' + value); // one = 1, two = 2, three = 3
}
barMap.forEach(function(value, key) {
console.log(key + ' = ' + value); // one = 1, two = 2, three = 3
});Set
A Set object is a collection of values. A value in a set may only
occur once. You can iterate its elements in insertion order.
let bazSet = new Set();
bazSet.add('a');
bazSet.add('b');
bazSet.add('c');
for (let value of bazSet) {
console.log(value); // a, b, c
}WeakMap
A WeakMap is a collection of key/value pairs in which the keys are
weakly referenced. The keys must be objects. They allow for objects
which are no longer needed to be cleared from memory.
There is no method to obtain a list of the keys.
const foowm = new WeakMap();
const key1 = {};
foowm.set(key1, "value1");
foowm.get(key1); // "value1"WeakSet
A WeakSet is a collection of objects. References to objects are held weakly.
If no other references to an object stored in the WeakSet exist , those objects
can be garbage collected.
const barws = new WeakSet();
const value1 = {};
barws.add(value1);References
Categories :
JavaScript
ES6