ES6 ~ Data Types & Structure - rohit120582sharma/Documentation GitHub Wiki

Set

Sets are a new object type (data-structure) that allow to create collections of unique values. The values in a set can be either simple primitives like strings or integers, but more complex object types like objects or arrays.

let alphabets = new Set();

alphabets.add('a');
alphabets.add('b');
alphabets.add('d');
alphabets.add('c');
alphabets.add('a');

alphabets.delete('d');

for (let alphabet of alphabets) {
    console.log(alphabet); // a, b, c
}


Map

Maps are a new object type (data-structure) that allow to store collections of key-value pairs and remembers the original insertion order of the keys.

Object is similar to Map in that both let you set keys to values, retrieve those values, delete keys, and detect whether something is stored at a key. However, there are important differences that make using a Map preferable in certain cases:

  • The keys of an Object are String and Symbol, whereas they can be any value for a Map, including functions, objects, and any primitive.
  • Map can be iterated in the order in which the values were added, contrary to object where there’s no guarantee about the order.
  • You can get the size of a Map easily with the size property, while the number of properties in an Object must be determined manually.
  • A Map is an iterable and can thus be directly iterated, whereas iterating over an Object requires obtaining its keys in some fashion and iterating over them.
let activities = new Map();

activities.set(1, 'πŸ‚');
activities.set(2, '🏎');
activities.set(3, '🚣');
activities.set(4, '🀾');

for (let [nb, activity] of activities) {
    console.log(`Activity ${nb} is ${activity}`);
}
⚠️ **GitHub.com Fallback** ⚠️