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
Let us understand the concept of set by using the following example:
Example
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
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
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
Output
7 Set { 'Green', 'Red', 'Orange', 'Yellow', 'Violet', 'Indigo', 'Blue' }
Set.prototype.clear()
It clears all the objects from the sets.
Example
Output
0
Set.prototype.delete(value)
This method is used to remove the corresponding passed value from the set object.
Example
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
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
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
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
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
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
Output
{ value: 'Green', done: false } { value: [ 'Green', 'Green' ], done: false } { value: 'Green', done: false }