ES6 Set

A set is a data structure that allows you to create a collection of unique values. Sets are the collections that deal with single objects or single values.

Set is the collection of values similar to arrays, but it does not contain any duplicates. It allows us to store unique values. It supports both primitive values and object references.

As similar to maps, sets are also ordered, i.e., the elements in sets are iterated in their insertion order. It returns the set object.

Syntax

snippet
var s = new Set("val1","val2","val3");

Let us understand the concept of set by using the following example:

Example

snippet
let colors = new Set(['Green', 'Red', 'Orange', 'Yellow', 'Red']);
console.log(colors);

All the elements of a Set must be unique. Therefore the set colors in the above example only contain four distinct elements. We will get the following output after the successful execution of the above code.

Output

Output
Set { 'Green', 'Red', 'Orange', 'Yellow' }

Let us see the properties and methods of the Set.

Set Properties

S.no. Properties Description
1. Set.size This property returns the number of values in the set object.

Set.size

This property of the Set object returns the value that represents the number of elements in the Set object.

Example

snippet
let colors = new Set(['Green', 'Red', 'Orange', 'Yellow', 'Red']);
console.log(colors.size);
console.log(colors);

Output

Output
4 Set { 'Green', 'Red', 'Orange', 'Yellow' }

Set Methods

The set object includes several methods, which are tabulated as follows:

S.no. Methods Description
1. Set.prototype.add(value) It appends a new element to the given value of the set object.
2. Set.prototype.clear() It removes all the elements from the set object.
3. Set.prototype.delete(value) It removes the element which is associated with the corresponding value.
4. Set.prototype.entries() It returns a new iterator object, which contains an array of each element in the Set object in insertion order.
5. Set.prototype.forEach(callbackFn[, thisArg]) It executes the callback function once.
6. Set.prototype.has(value) This method returns true when the passed value is in the Set.
7. Set.prototype.values() It returns the new iterator object, which contains the values for each element in the Set, in insertion order.

Now, we are going to understand the above methods of the Set object in detail.

Set.prototype.add(value)

This method is used to append a new element with the existing value to the Set object.

Example

snippet
let colors = new Set(['Green', 'Red', 'Orange', 'Yellow', 'Red']);
colors.add('Violet');
colors.add('Indigo');
colors.add('Blue');
colors.add('Violet');
console.log(colors.size);
console.log(colors);

Output

Output
7 Set { 'Green', 'Red', 'Orange', 'Yellow', 'Violet', 'Indigo', 'Blue' }

Set.prototype.clear()

It clears all the objects from the sets.

Example

snippet
let colors = new Set(['Green', 'Red', 'Orange', 'Yellow', 'Red']);
colors.add('Violet');
colors.add('Indigo');
colors.add('Blue');
colors.add('Violet');
colors.clear()
console.log(colors.size);

Output

Output
0

Set.prototype.delete(value)

This method is used to remove the corresponding passed value from the set object.

Example

snippet
let colors = new Set(['Green', 'Red', 'Orange', 'Yellow', 'Red']);
colors.add('Violet');
colors.add('Indigo');
colors.add('Blue');
colors.add('Violet');
colors.delete('Violet');
console.log(colors.size);
console.log(colors);

Output

Output
6 Set { 'Green', 'Red', 'Orange', 'Yellow', 'Indigo', 'Blue' }

Set.prototype.entries()

It returns the object of a new set iterator. It contains an array of values for each element. It maintains the insertion order.

Example

snippet
let colors = new Set(['Green', 'Red', 'Orange', 'Yellow', 'Red']);
colors.add('Violet');
colors.add('Indigo');
colors.add('Blue');
colors.add('Violet');
var itr = colors.entries();
for(i=0;i<colors.size;i++)  {
    console.log(itr.next().value); 
}

Output

Output
[ 'Green', 'Green' ] [ 'Red', 'Red' ] [ 'Orange', 'Orange' ] [ 'Yellow', 'Yellow' ] [ 'Violet', 'Violet' ] [ 'Indigo', 'Indigo' ] [ 'Blue', 'Blue' ]

Set.prototype.forEach(callbackFn[, thisArg])

It executes the specified callback function once for each Map entry.

Example

snippet
let colors = new Set(['Green', 'Red', 'Orange', 'Yellow', 'Red']);
colors.add('Violet');
colors.add('Indigo');
colors.add('Blue');
colors.add('Violet');
function details(values){
    console.log(values);
}
colors.forEach(details);

Output

Output
Green Red Orange Yellow Violet Indigo Blue

Set.prototype.has(value)

It returns the Boolean value that indicates whether the element, along with the corresponding value, exists in a Set object or not.

Example

snippet
let colors = new Set(['Green', 'Red', 'Orange', 'Yellow', 'Red']);
colors.add('Violet');
colors.add('Indigo');
colors.add('Blue');
colors.add('Violet');
console.log(colors.has('Indigo'));
console.log(colors.has('Violet'));
console.log(colors.has('Cyan'));

Output

Output
true true false

Set.prototype.values()

It returns a new iterator object that includes the values for each element in the Set object in the insertion order.

Example

snippet
let colors = new Set(['Green', 'Red', 'Orange', 'Yellow', 'Red']);
colors.add('Violet');

var val = colors.values();
console.log(val.next().value); 
console.log(val.next().value); 
console.log(val.next().value); 
console.log(val.next().value); 
console.log(val.next().value);

Output

Output
Green Red Orange Yellow Violet

Weak Set

It is used to store the collection of objects. It is similar to the Set object, so it also cannot store duplicate values. Similar to weak maps, weak sets cannot be iterated. Weak sets can contain only objects which may be garbage collected.

Weak set only includes add(value), delete(value) and has(value) methods of Set object.

Example

snippet
'use strict' 
   let ws = new WeakSet();  
   let obj = {msg:"Welcome Back!"}; 
   ws.add(obj); 
   console.log(ws.has(obj)); 
   ws.delete(obj); 
   console.log(ws.has(obj));

Output

Output
true false

Iterators

An iterator is an object which defines the sequence and a return value upon its termination. It allows accessing a collection of objects one at a time. Set and Map both include the methods that return an iterator.

Iterators are the objects with the next() method. When the next() method gets invoked, the iterator returns an object along with the 'value' and 'done' properties.

The 'done' is a Boolean which returns true after reading all of the elements in the collection. Otherwise, it returns false.

Let's understand the implementations of iterator along with the Set object.

Example

snippet
let colors = new Set(['Green', 'Red', 'Orange', 'Yellow', 'Red']);
var itr = colors.keys();
var itr1 = colors.entries();
var itr2 = colors.values();
console.log(itr.next());
console.log(itr1.next());
console.log(itr2.next());

Output

Output
{ value: 'Green', done: false } { value: [ 'Green', 'Green' ], done: false } { value: 'Green', done: false }
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents