In TypeScript, you can use the map()
method to convert each integer in the set to a string.
In this example,
intSet
.map()
method to iterate over each element of the set and convert it to a string using the toString()
method.map()
method returns a new array with the transformed elements.new Set()
constructor.const intSet: Set<number> = new Set([1, 2, 3, 4, 5]);
const stringSet: Set<string> = new Set([...intSet].map(num => num.toString()));
console.log('Set of strings:', stringSet);
Set of strings: Set(5) { '1', '2', '3', '4', '5' }
In this example,
intSet
.stringSet
.intSet
using the forEach()
loop.toString()
method and add it to the stringSet
.const intSet: Set<number> = new Set([1, 2, 3, 4, 5]);
const stringSet: Set<string> = new Set();
intSet.forEach(num => stringSet.add(num.toString()));
console.log('Set of strings:', stringSet);
Set of strings: Set(5) { '1', '2', '3', '4', '5' }
In this tutorial, we learned How to Convert a Set of Integers to a Set of Strings in TypeScript language with well detailed examples.