JavaScript split string to array of int
Solution 1:
var ArrayData = $('#TheData').html().split(',').map( Number );
Add Array.prototype.map()
to older browsers with the code from MDN.
You can use jQuery's $.map()
in the same manner, though it won't work with $.prototype.map()
.
var ArrayData = $.map( $('#TheData').html().split(','), Number );
Solution 2:
var ArrayData = $.map($('#TheData').text().split(','), function(value){
return parseInt(value, 10);
// or return +value; which handles float values as well
});
You can use $.map
to transform the array of strings to ints by calling parseInt
on each of the elements in the array