Definition of TypeScript Set Method
In TypeScript, a novel data structure has been added to ES6 JavaScript version known as set. This helps in storing data of distinct values that occurs only once into a list that is available in different programming languages like C#, Java, etc. Even though this data structure is almost similar to maps, it cannot store pairs of key-values, but only keys. As the objects of set are a group of values, it can be iterated through those items in a particular order. In this article, we will be discussing the different aspects of set in typescript.
Syntax:
Below is the syntax of typescript set.
let setobj = new Set();
Here, set type has to be used along with the keyword new.
Methods
Following are the different methods of set in Typescript.
- Method:
add(val)
Description: This method helps in adding values to the set.
- Method:
has(val)
Description: This method checks whether the value passed in the method is present in the set or not. If it is present, true will be returned. Else, false will be returned.
- Method:
delete()
Description: This method helps in removing values from the set.
- Method:
size()
Description: This method helps in returning size of the set.
- Method:
clear()
Description: This method helps in removing all values from the set.
Examples
Let us discuss Examples of TypeScript Set.
Example #1: Typescript program that creates a set and add values to it.
Code:
//create a set with marks as object
let marks = new Set();
//Add marks to the set
marks.add(21);
marks.add(23);
marks.add(13);
console.log("The marks in the Set are:");
console.log(marks);
Sample Output:
In this program, a set is created with marks as object. Then, elements such as 21, 23, and 13 are added to it. On executing the code, all elements in the set are displayed.
Example #2: Typescript program that creates a set and add values to it using add() method chaining
Code:
//create a set with marks as object
let marks = new Set();
//Add marks to the set
marks.add(21).add(23).add(13);
console.log("The marks in the Set are:");
console.log(marks);
Sample Output:
In this program also, a set is created with marks as object. Then, elements such as 21, 23, and 13 are added to it. The difference of this program with the above program is that here the elements are added to the set by chaining the add() method. On executing the code, all elements in the set are displayed.
Example #3: Typescript program that creates a set and checks whether a particular value is present in it.
Code:
//create a set with marks as object
let marks = new Set();
//Add marks to the set
marks.add(21).add(23).add(13);
console.log("The marks in the Set are:");
console.log(marks);
//Check whether the value 21 and 34 is present in the set or not
console.log(marks.has(21));
console.log(marks.has(34));
Sample Output:
In this program also, a set is created with marks as object. After adding the elements such as 21, 23, and 13 are added to it, elements 21, and 34 are checked whether they are present in the available set. As element 21 is present and 34 is absent, true and false will be returned respectively.
Example #4: Typescript program that creates a set and finds the size of the set.
Code:
//create a set with marks as object
let marks = new Set();
//Add marks to the set
marks.add(21).add(23).add(13);
console.log("The marks in the Set are:");
console.log(marks);
//returns the size of the set
console.log("The size of the set is:");
console.log(marks.size);
Sample Output:
In this program also, a set is created with marks as object. After adding the elements such as 21, 23 and 13 are added to it, size of the set is identified using the size() method as shown in the program. As there are three elements in the set, 3 will be returned as the size of the set.
Example #5: Typescript program that creates a set and removes a particular element from it.
Code:
//create a set with marks as object
let marks = new Set();
//Add marks to the set
marks.add(21).add(23).add(13) ;
console.log("The marks in the Set are:") ;
console.log(marks);
//returns the size of the set
console.log("The size of the set is:");
console.log(marks.size) ;
//deletes an element from the set
console.log(marks.delete(23) ) ;
console.log("The updated size of the set is:") ;
console.log(marks.size) ;
console.log("The updates elements in the Set are:");
console.log(marks);
Sample Output:
In this program also, a set of marks created with elements such as 21, 23, and 13. Then, the size of the set is identified using the size() method. Once this is done, element 21 is removed and again the size of the set is identified. On executing the code, it can be seen that size of the set is reduced to 2 and the updated set does not contain 21 as element.
Example #6: Typescript program that creates a set and removes all the elements from it.
Code:
//create a set with marks as object
let marks = new Set();
//Add marks to the set
marks.add(21).add(23).add(13);
console.log("The marks in the Set are:");
console.log(marks) ;
//returns the size of the set
console.log("The size of the set is:") ;
console.log(marks.size) ;
//clears all elements from the set
marks.clear() ;
console.log("The updated size of the set is:") ;
console.log(marks.size) ;
console.log("The updates elements in the Set are:") ;
console.log(marks) ;
Sample Output:
In this program, a set of marks created with elements such as 21, 23, and 13. Then, the size of the set is identified using the size() method. Once this is done, all elements are removed, and again the size of the set is identified. On executing the code, it can be seen that size of the set is reduced to 0 and the updated set does not contain any elements.
Conclusion
A data structure known as set has been added to ES6 JavaScript that helps in storing data of distinct values that occurs only once into a list that is available in different programming languages like C#, Java, etc. In this article, different aspects such as syntax, methods, and examples of set in typescript are explained in detail.
Recommended Articles
We hope that this EDUCBA information on “TypeScript Set” was beneficial to you. You can view EDUCBA’s recommended articles for more information.