Set in ES6
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
var q = new Set (“value1”, “value2”, “value3”);
Example
Here, we have an example to illustrate the concept of set.
let month = new Set ([“January”, “February”, “March”, “April”, “May”]);
console.log (month);
Output
After the execution of the code, we got the following output:
Methods of Sets
The set object has various methods which are given in tabular form-
Sr. No. | Method | Description |
1 | Set.prototype.clear() | This method is used to delete all the elements of the set object. |
2 | Set.prototype.add(value) | It is used to affix a new element in the existing value of the set object. |
3 | Set.prototype.delete(value) | This method is used to delete the element attached to the value. |
4 | Set.prototype.has(value) | It is used to return true when the passed value lies in the set object. |
5 | Set.prototype.entries() | This method is used to return the iterator object. This object has an array of set object elements in insertion order. |
6 | Set.prototype.values() | It is used to return the iterator object, includes an array of set object elements in insertion order. |
7 | Set.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-
let days = new Set ([‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’]); days.add(‘Friday’); days.add(‘Saturday’); days.add(‘Friday’); days.add(‘Thursday’); days.clear(); console.log(days.size);
Output
After the execution of the above code, we got the following output:
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-
let days = new Set ([‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’]); days.add(‘Friday’); days.add(‘Saturday’); days.add(‘Thursday’); days.add(‘Friday’); days.clear(); console.log(days.size);
Output
After the execution of the above code, we got the following output:
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-
let days = new Set ([‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’]); days.add(‘Friday’); days.add(‘Saturday’); days.add(‘Thursday’); days.add(‘Friday’); days.delete(‘Friday’); console.log(days.size); console.log(days);
Output
After the execution of the above code, we got the following output:
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-
let days = new Set ([‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’]); days.add(‘Friday’); days.add(‘Saturday’); days.add(‘Thursday’); days.add(‘Friday’); console.log(days.has(‘ Friday’); console.log(days.has(‘Saturday’); console.log(days.has(‘Sunday’);
Output
After the execution of the above code, we got the following output:
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-
let days = new Set ([‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’]); days.add(‘Friday’); days.add(‘Saturday’); days.add(‘Thursday’); days.add(‘Friday’); var iterator = days.entries(); for(i=0;i<days;i++) { console.log(iterator.next().value); }
Output
After the execution of the above code, we got the following output:
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-
let days = new Set ([‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’]); days.add(‘Thursday’); var value = days.values(); console.log(value.next().value); console.log(value.next().value); console.log(value.next().value); console.log(value.next().value); console.log(value.next().value);
Output
After the execution of the above code, we got the following output:
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-
let days = new Set ([‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’]); days.add(‘Friday’); days.add(‘Saturday’); days.add(‘Thursday’); days.add(‘Friday’); function details(values) { console.log(values); } days.forEach(details);
Output
After the execution of the above code, we got the following output:
Properties of Set
Sr. No. | Properties | Description |
1 | Set.size | This 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
let month = new Set([‘January’, ‘February’, ‘March’, ‘April’, ‘February’]); console.log(month.size); console.log(month);
Output
After the execution of the above code, we got the following output:
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.
‘use strict’ let weakest = new weak Set(); let object = {message: “Welcome to ES6!”}; weakset.add(object); console.log(weakest.has(object)); weakest.delete(object); console.log(weakest.has(object));
Output
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.
let month = new Set([‘January’, ‘February’, ‘March’, ‘April’, ‘May’]); var iterator = months.keys(); var iterator = months.entries(); var iterator = months.values(); console.log(iterator.next()); console.log(iterator1.next()); console.log(iterator2.next());
Output
After the execution of the above example, we got the following output: