String with Comma separated numbers to array of integer in javascript
I would recommend this:
var array;if (string.length === 0) { array = new Array();} else { array = string.replace(/, +/g, ",").split(",").map(Number);}
The string.replace(/, +/g, ",").split(",")
returns an array with one item - an empty string. In javascript, empty string when converted to number is 0. See yourself
Number(""); // returns (int)0
To remove the spaces after the comma you can use regular expressions inside the split function itself.
array = string.split(/\s*,\s*/).map(Number);
I hope it will help