0
How to convert string to array?
I want to convert a string separated by commas and spaces into Number array in JavaScript.. Ex: "2, 44, -7, 8" -> [ 2, 44, -7, 8 ] And whenever I convert string to array it shows output : [2, 4, 4, NaN, 8]
3 Answers
+ 3
var arr = "2, 44, -7, 8".split(/\s*,\s*/).map(s => Number(s));
+ 2
I used split() with a regex matching any number of spaces + comma + any number of spaces to act as separator...
this return a list of numbers stored as string.
then I map that list to another one to convert the strings to numbers ;)
0
Can you explain your code? visph