To convert a set of strings to a set of floats in TypeScript, you can use the map()
method along with the parseFloat()
function to transform each string value into its corresponding float value.
In this example,
stringArray1
with values ["1.1", "2.2", "3.3"]
.map()
method on stringArray1
to transform each string value into its corresponding float value. We pass a function to map()
that converts each string value to a float using parseFloat()
.floatSet1
.const stringArray1: string[] = ["1.1", "2.2", "3.3"];
const floatSet1: Set<number> = new Set(stringArray1.map(parseFloat));
console.log(floatSet1);
{1.1, 2.2, 3.3}
In this example,
stringArray2
with values ["4.5", "5.6", "6.7"]
.map()
method on stringArray2
to transform each string value into its corresponding float value. We pass a function to map()
that converts each string value to a float using parseFloat()
.floatSet2
.const stringArray2: string[] = ["4.5", "5.6", "6.7"];
const floatSet2: Set<number> = new Set(stringArray2.map(parseFloat));
console.log(floatSet2);
{4.5, 5.6, 6.7}
In this tutorial, we learned How to Convert a Set of Strings to a Set of Floats in TypeScript language with well detailed examples.