0

Set and Map of ES6

Set

The Set lets you store unique values of any type (it can be primitive or object references types)

Let us learn and understand more by looking from the below snippet code

var set = new Set();
set.add(1);
set.add(2);
set.add(3);

console.log(set.size);//3
console.log(set.has(1));//true
console.log(set.has('1'));//false
console.log(set.has(22));//false

In the above snippet the object is created and 3 items are added to it. It has property to check the size and method ‘has’ to check if item already added.

var set = new Set();
set.add(1);
set.add(1);
set.add(1);

console.log(set.size);//1

Even though the code tries to add duplicate values to the object it is not throwing exception and size remains 1

var set = new Set();
set.add(1);
set.add(2);
set.add(1);
set.add(1);

console.log(set.size);//2

set.delete(1);
console.log(set.size);//1
console.log(set);//Set { 2 }

In the above code an item added is removed using delete method.

var items = new Set([1, 2, 3, 3, 5, 5]);
console.log(items);//Set { 1, 2, 3, 5 }
console.log(items.size);//4

The Set constructor accepts array as parameter and it ensures that there is no duplicate entries.

var items = new Set([1, 2, 3, 4, 5]);
var val = items.values();
console.log(val.next().value);//1
console.log(val.next().value);//2

var val2 = items.values();
console.log(val2.next().value);//1
console.log(val2.next().value);//2

The values() method actually returns new Iterator object that contains the values for each element

Map

The Map object holds key-value pairs. Any type (it can be primitive or object references types) may be used as either a key or a value. Let us learn and understand more by looking from the below snippet code

var map = new Map();
map.set('name', 'Shil');
console.log(map.get('name'));//Shil

map.set('name', 'Joe');
console.log(map.get('name'));//Joe

console.log(map.size);//1

Here key value is added to the Map with value ‘Shil’ latter it is updated with new value of ‘Joe’. It actually updates the value accordingly.

map.set(100, true);
console.log(map.has(100));//true
console.log(map.has('100'));//false
console.log(map.get(100));//true

console.log(map.has(999));//false
console.log(map.get(999));//undefined

Here if you notice 100 (number) is not same as ‘100’(string). The code is trying to fetch the value for the non existing key but it is not throwing exception instead it return undefined. Those who are from C# background might why am I have mentioned about ‘exection’. In C#, if you try to access the non existing key it will throw an exception 🙂

console.log(map.keys());//MapIterator { 'name', 100 }
console.log(map.values());//MapIterator { 'Joe', true }

The methods keys() and values() will return new iterator object

map.delete(100);
console.log(map.size);//1
console.log(map.keys());//MapIterator { 'name' }

So here comes the way to remove the key-value from Map

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.