I am converting String with Comma separated numbers to a array of integer like,
var string = "1,2,3,4";
var array = string.replace(/, +/g, ",").split(",").map(Number);
it returns array = [1,2,3,4];
But when ,
var string = "";
var array = string.replace(/, +/g, ",").split(",").map(Number);
it returns array = [0];
I was expecting it to return array = [];
can someone say why this is happening.
I would recommend this:
var array;
if (string.length === 0) {
array = new Array();
} else {
array = string.replace(/, +/g, ",").split(",").map(Number);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With