Maps and Sets

This feature helps us to map one to one objects in JavaScript.

This has been long missing from JavaScript and enables to create simple object to object maps with O(1) access time.

The Map object is a simple key/value map.

It is possible to use it as follows:

var someMap = new Map();
var keyString = "mykey",
    keyObj = {}, 
    keyFunc = function () {};

//performing setters
someMap.set(keyString, "some string");
someMap.set(keyObj, "another string");
someMap.set(keyFunc, "yet another");

So everything that is typeof object can be a key in the map (string, object, function).

The map also supports the Symbol.iterator which helps to iterate on the map with for..of as follows:

for (var v of someMap) 
{
  console.log(v);

}

results matching ""

    No results matching ""