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.
var s = new Set("val1","val2","val3");
Let us understand the concept of set by using the following example:
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.
Let us see the properties and methods of the Set.
S.no. | Properties | Description |
---|---|---|
1. | Set.size | This property returns the number of values in the set object. |
This property of the Set object returns the value that represents the number of elements in the Set object.
let colors = new Set(['Green', 'Red', 'Orange', 'Yellow', 'Red']); console.log(colors.size); console.log(colors);
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.
This method is used to append a new element with the existing value to the Set object.
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);
It clears all the objects from the sets.
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);
This method is used to remove the corresponding passed value from the set object.
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);
It returns the object of a new set iterator. It contains an array of values for each element. It maintains the insertion order.
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); }
It executes the specified callback function once for each Map entry.
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);
It returns the Boolean value that indicates whether the element, along with the corresponding value, exists in a Set object or not.
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'));
It returns a new iterator object that includes the values for each element in the Set object in the insertion order.
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);
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.
'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));
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.
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());