Navigate back to the homepage

How to convert string array to number array in typescript?

January 28th, 2022 · 1 min read
keep affilitate

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.

1let strArr = string = "1,2,3,4,5,6,7,8,9,10";
3strArr.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.

1let str = string = "1,2,3,4,5,6,7,8,9,10";
2str.split(',').map(function(item) {
3 return parseInt(item);
4}); // [ 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.

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

Thanks for reading…

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

melio affilitate

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide



More articles from Infinitbility

how to remove curly braces from string in javascript?

Javascript, delete curly braces from string example.

January 24th, 2022 · 1 min read

How to check react native version in CMD?

React Native, check version in CMD example.

January 20th, 2022 · 1 min read
© 2020–2022 Infinitbility
Link to $ to $ to $ to $ to $ to $mailto:[email protected]