Typescript Set
Set object is a new collection or data structure added in Javascript ES6. A set is a collection which lets you store unique values.
In this article, we will take a deep dive into typescript Set and cover the following topics
Adding values
Removing values
Checking values
Iterating through set
Emptying a set
Count set length
A set can be created using its constructor and new operator as shown below
// define a set const s = new Set();
where s is the reference variable which is used to interact with the set for performing any further operations.
Adding values to set
Values can be added to a set using its add()
method.
add()
accepts a single argument, which is the value to be added to the set. Example,
const nums = new Set(); nums.add(1); nums.add(2); nums.add(3); nums.add(4);
The values is added to the set only if it does not already exists in the set.
add()
returns the set with added value. So, to add multiple values at once, you can also chain it multiple times. So, above code can be replaced with
const nums = new Set(); nums.add(1).add(2).add(3).add(4);
You can even add objects to a set. Example,
const s = new Set(); s.add({x:1, y:z});
Values from a set can be removed with its
delete()
method.delete()
accepts an argument, which is the value to be removed from the set. It returns true if the element was removed from the set and false otherwise. Example, const nums = new Set(); nums.add(1).add(2).add(3).add(4); console.log(nums); nums.delete(2); console.log(nums);
This prints
Set(4) {1, 2, 3, 4}
Set(2) {1, 3, 4}
If the value being removed does not exist in the set, then delete()
returns false
.
Check value presence
Typescript set contains has()
method which accepts an argument and checks if the argument value is present in it.
has()
returns true
, if the argument is present in the set and false
otherwise.
const nums = new Set(); nums.add(1).add(2).add(3); console.log(nums.has(1)); console.log(nums.has('ab'));
Output is
true
false
Iterating set
There are multiple ways to loop through a set. All the methods iterate set elements in the order of their insertion.
A. With for-of loop
A typescript set can be iterated with for-of
loop. Syntax is
for(let variable of set) {
}
where variable contains a set value in every iteration. Example,
const nums = new Set(); nums.add(1).add(2).add(3); for(let num of nums) { console.log(num); }
Output is
1
2
3
B. Using values() method
values()
method returns a new iterator object which provides the values of set in their insertion order. Example,
const nums = new Set(); nums.add(1).add(2).add(3); for(let num of nums.values()) { console.log(num); }
If you check the type of the object returned by values()
, then it is a SetIterator
.
C. Using keys() method
keys()
is the same as values()
method in case of set object.
D. Using entries() method
entries()
method returns an iterator object which contains an array of key and value. Key and value are same in case of a set object.
So, the loop variable in every iteration is an array of two values, which are same for a set. Example,
const nums = new Set(); nums.add(1).add(2).add(3); for(let num of nums.entries()) { console.log(num[0]); }
You can get key-value from entries()
method using enumerated syntax below
const nums = new Set(); nums.add(1).add(2).add(3); for(let [key,value] of nums.entries()) { console.log(value]); }
E. forEach() method
forEach()
method accepts a function as its argument. This function in turn accepts a single argument, which is the current set value in every iteration. Example,
const nums = new Set(); nums.add(1).add(2).add(3); nums.forEach(function(num) { console.log(num); });
Argument function can be replaced with an arrow function, which is also introduced in ES6 as shown below.
const nums = new Set(); nums.add(1).add(2).add(3); nums.forEach(num => { console.log(num); });
Typescript set contains
clear()
method, which removes all set elements at once. Example, const nums = new Set(); nums.add(1).add(2).add(3); console.log(nums); nums.clear(); console.log(nums);
Output is
Set(3) {1, 2, 3}
Set(0) {size: 0}
Counting set length
Length of a set means the total number of elements in it. Set contains a size
property which returns the total number of values in the set. Example,
const nums = new Set(); nums.add(1).add(2).add(3); console.log(nums.size); // 3
That is all on typescript set. Hope the article was useful.