Im working on a simple program and im trying to put user input in it and i want to know if theres an easy way of doing that.
Asked
Active
Viewed 91 times
1 Answers
1
You can split
on the comma and use map
along with the unary plus operator to convert each element to a number.
str.split(",").map(a=>+a);
const str = "1,2,3,4";
const arr = str.split(",").map(a=>+a);
console.log(arr);

Unmitigated
- 76,500
- 11
- 62
- 80
-
Thank you for this answer it should work if i implement it correctly. Hope you have a nice day. – Elle Jun 10 '20 at 23:21
-
@The_Duck_Lord No problem. – Unmitigated Jun 10 '20 at 23:22
-
did it. Also found other ways of splitting strings that are not numbers into arrays with the [spreadSyntax](https://stackoverflow.com/a/33233956/2864502) though this one is used for splitting letters in a string. Like `hello` to [`h`,`e`,`l`,`l`,`o`] . – Elle Jun 11 '20 at 12:23