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.
In this example, we use split()
method with comma (','
) as separator to split string with commas into array of strings.
xxxxxxxxxx
1
var text = '1,2,3';
2
3
var array = text.split(',');
4
5
console.log(array); // [ '1', '2', '3' ]
In this example, we additionally use map()
method to convert all elements from the splitted array into Number
type.
xxxxxxxxxx
1
var text = '1,2,3';
2
3
var array = text.split(',').map(Number);
4
5
console.log(array); // [ 1, 2, 3 ]