To convert a string of numbers to an array of numbers, we use the split
and map
methods.
For instance, we write
const a = "1,2,3,4";
const b = a.split(",").map(Number);
to call split
to split the a
string into a string array by the commas.
And then we call map
with Number
to convert each entry to a number and return them in a new array.