Collections - Map

Three new Collections: SET(like Array), MAP(Key-Value Object) and WEAKMAP(like Map, but different)

Map object

ECMAScript 6 introduces a new data structure to map values to values.

A Map object is a simple key/value map and can iterate its elements in insertion order.

map.set and map.get methods are used to set and retrive the values from the Map colloection.

Basic usage

var sayings = new Map(); sayings.set("dog", "woof"); sayings.set("cat", "meow"); sayings.set("elephant", "toot"); console.log(sayings.size); // 3 console.log(sayings.get("cat")); // meow console.log(sayings.get("fox")); // undefined console.log(sayings.has("bird")); // false for (var [key, value] of sayings) { console.log(key + " goes " + value); } // "cat goes meow" // "elephant goes toot" sayings.clear(); console.log(sayings.size); // 0

Traditionally, objects have been used to map strings to values.

Objects allow you to set keys to values, retrieve those values, delete keys, and detect whether something is stored at a key.

Map objects, however, have a few more advantages that make them better maps.