Maps and WeakMaps with ES6

The Map object holds key-value pairs. Any value (both objects and primitive values) may be used as either a key or a value.

API For Map In ES6

1. set()
2. get()
3. size()
4. clear()
5. has()

var myMap = new Map();

myMap.set(‘name’,’ravi’);

myMap.set(‘age’,’22');

console.log(myMap.get(‘name’));

console.log(myMap.size);

console.log(myMap.has(‘name’));

Iterators For Map In ES6

1. Keys()
2. entries()
3. values()

for(var key of myMap.keys() )
 console.log(key);

for(var value of myMap.values() )
 console.log(value);

for(var [key , value] of myMap.entries() )
 console.log(key + ‘:’ + value);

The WeakMap object is a collection of key/value pairs in which the keys are weakly referenced. The keys must be objects and the values can be arbitrary values.

var myMap = new WeakMap();

var myObject ={};

myMap.set(myObject,’ravi’);

var setkeyValue = function (){
 
}

myMap.set(setkeyValue,’22');

console.log(myMap.get(myObject));

console.log(myMap.get(setkeyValue));

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.