EN
JavaScript - convert string with commas to array
0
points
In this article, we would like to show you how to convert string with commas to array in JavaScript.
1. Convert string to array of strings
In this example, we use split() method with comma (',') as separator to split string with commas into array of strings.
// ONLINE-RUNNER:browser;
var text = '1,2,3';
var array = text.split(',');
console.log(array); // [ '1', '2', '3' ]
2. Convert string to array of numbers
In this example, we additionally use map() method to convert all elements from the splitted array into Number type.
// ONLINE-RUNNER:browser;
var text = '1,2,3';
var array = text.split(',').map(Number);
console.log(array); // [ 1, 2, 3 ]