Hello Friends đź‘‹,

Welcome To Infinitbility! ❤️

Today, we will learn how to convert string array to array of numbers in typescript.

TypeScript have split() method which will make string to array but still all number in array in string.

let strArr = string = "1,2,3,4,5,6,7,8,9,10";

strArr.split(","); // [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ]

Now, we know how to make array from string but we also want to convert value in number while converting string to array.

let str = string = "1,2,3,4,5,6,7,8,9,10";
str.split(',').map(function(item) {
    return parseInt(item);
}); // [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

In above example, we have first converted string to array then loop whole array element and convert it on number one by one.

You can also below example for shorthand code.

let str = string = "1,2,3,4,5,6,7,8,9,10";
Array.from(str.split(','), Number) // [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

Thanks for reading…