Warning: session_start(): open(/tmp/sess_5bf8eb8c4f546e3586f31b47f5a49f7f, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Convert A String To Array Of Numbers In JavaScript - LearnShareIT

How To Convert A String To Array Of Numbers In JavaScript

Convert a String to Array of Numbers in JavaScript

We will learn how to convert a string to array of numbers in JavaScript using two different approaches. Each approach is relatively straightforward, and you can choose whichever you want. Let’s read this article now.

Convert a string to array of numbers in JavaScript

Using split() and for loop

When split() is called, it searches a string for a specific pattern and divides the string into multiple substrings. These substrings are placed into an array and the array is then returned:

Syntax:

split(separator)

Parameter:

  • split: The pattern that describes where to split.

First, assume your string has a list of numbers which is separated by semicolons. Then use split() to achieve an array of splitted strings. After that convert each string in the array to number type and push it to the destination array:

let s = "1;3;5;7";
let a = s.split(";");
let res = [];

for (i of a)
    res.push(Number(i));

console.log(res);

Output: 

[1,3,5,7]

If a string contains multiple separators instead of one, you cannot declare as above in the split()’s parameter. All you have to do is to use a regex to represent your list of separators (you can read more at our tutorial). For example:

let s = "1,3;5:7 9.9";
let a = s.split(/[;,:\s]+/);
let res = [];

for (i of a)
    res.push(+i);

console.log(res);

The above code tries to split a string by 4 separators: a semicolon(;), a comma (,) a colon(:) and a whitespace but not a dot (as it represents a floating point number). We also introduced another way of converting a string to a number using + operator inside the for loop.

Output:

[1, 3, 5, 7, 9.9]

This method is not only easy to understand but also easy to implement. However, there is also an approach that can help you do this with just a line of code:

Using split() and map()

Syntax:

map(callbackFn)

Parameter:

  • callbackFn: The callbackFn function is called for each element, and its returned value is added to newArray.

When the map method is called, a new array is created that contains the results of applying the callbackFn function provided to every element of the calling array:

let s = "1;3;5;7";
res = s.split(";").map(Number)
console.log(res);

Output:

[1,3,5,7]

Number() converts values of other types into numbers. Constants and methods in the Number constructor help you work with numbers. Number(value) converts a string, floating-point number, boolean value, or null value to the Number type. If the input value can’t be converted, NaN is returned.

Summary

We have learned how to convert a set to an array in JavaScript using two different solutions. It would help if you considered that each approach has its pros and cons. We hope you will be successful using our tutorial. Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *