ES6 SetA 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. SyntaxLet us understand the concept of set by using the following example: ExampleAll 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
Set.sizeThis 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 MethodsThe set object includes several methods, which are tabulated as follows:
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 SetIt 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. ExampleOutput true false IteratorsAn 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. ExampleOutput { value: 'Green', done: false } { value: [ 'Green', 'Green' ], done: false } { value: 'Green', done: false } Next TopicES6 Classes |