Set in ES6

A Set is a kind of object used to create a group of unique values. The values stored in a set can be string, literal, array, and integer. The set only deal with a single value or single object (Object reference).

The set is similar to an array, but it doesn’t involve any duplicate element. The sets are always defined in an ordered form similar to maps. It means the set elements are iterated in an insertion manner and used to return the set object.

Syntax

Example  

Here, we have an example to illustrate the concept of set.

console.log (month);

Output

After the execution of the code, we got the following output:

Set in ES6

Methods of Sets

The set object has various methods which are given in tabular form-

             Sr. No.            Method          Description
                 1Set.prototype.clear()This method is used to delete all the elements of the set object.
                 2Set.prototype.add(value)It is used to affix a new element in the existing value of the set object.
                 3Set.prototype.delete(value)This method is used to delete the element attached to the value.
                 4Set.prototype.has(value)It is used to return true when the passed value lies in the set object.
                 5Set.prototype.entries()This method is used to return the iterator object. This object has an array of set object elements in insertion order.
                 6Set.prototype.values()It is used to return the iterator object, includes an array of set object elements in insertion order.
                 7Set.prototype.forEach(callbackFn[, thisArg])This method is used to run the callback function once only.

Here, we have a detailed description of each method.

Set.prototype.clear()

This particular method is used to delete all the objects of the sets.

Example

We have an example to understand this method-

Output

After the execution of the above code, we got the following output:

Set in ES6

Set.prototype.add(value)

The Set.prototype.add(value) method is used to affix the new elements with set object values.

Example

Here, we have an example to understand this method-

Output

After the execution of the above code, we got the following output:

Set in ES6

Set.prototype.delete(value)

This method is used to delete the element attached to the set object.

Example

Here, we have an example to understand this method-

Output

After the execution of the above code, we got the following output:

Set in ES6

Set.prototype.has(value)

This method is used to return true or false that defines that the element exists in the Set object or not.   

Example

Here, we have an example to understand this method-

Output

After the execution of the above code, we got the following output:

Set in ES6

Set.prototype.entries()

This method is used to return the object set of new iterators. It includes the array value for each element. The insertion order follows this insertion method.

Example

Here, we have an example to understand this method-

Output

After the execution of the above code, we got the following output:

Set in ES6

Set.prototype.values()

This method is used to return the iterator object which includes an array of set object elements in insertion order.

Example

Here, we have an example to understand this method-

Output

After the execution of the above code, we got the following output:

Set in ES6

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

This method is used to execute the particular callback function once only.

Example

Here, we have an example to understand this method-

Output

After the execution of the above code, we got the following output:

Set in ES6

Properties of Set

            Sr. No.           Properties           Description
                 1Set.sizeThis property is used to return the values in the set object.

Set.size

It is a property of the set object. This specified property is used to return the value, which presents the elements in the set object.

Example

Output

After the execution of the above code, we got the following output:

Set in ES6

Weak Set

The weak set helps us to store the group of objects. Weak set contains add(value), has(value), delete(value) set object methods. The weak sets are similar to set objects, but they cannot store the replicate values. We cannot iterate the weak set similar to weak maps. The objects which are stored in a weak set only can be in garbage collection.

Example

Here, we have the following example to elaborate the weak set.

Output

Set in ES6

Iterator

The iterator can be defined as an object, used to determine the sequence and return a value at the time of termination. The iterator allows to access the set of one object at the same time.

The Set object and Map object both have the methods that return an iterator. The Iterator can be considered as objects with the next method (). When we call the next method, then the iterator returns an object that includes the value and done properties. The done returns a Boolean value; true if it accesses all elements of the set object otherwise false.  

Example

We have an example to illustrate the iterator implementation with the Set object.

Output

After the execution of the above example, we got the following output:

Set in ES6

Pin It on Pinterest

Share This