字符串连接留下"在开始

时间:2016-03-20 16:10:27

标签: javascript string split concatenation

我正在通过连接创建一个名为temp的字符串

var temp = ""; //this is what we will send to the server

for(var i = 1; i < 25; i++){
    console.log("checking" + i);
    if($('[name = '+ i + "]").is(":checked")){

        console.log("in the if");
        temp = temp + hourArrayOpen[i - 1] + " - " + hourArrayClose[i - 1] + ",";
    }
} 

在客户端,此字符串打印出来:

11:00am - 12:00pm,12:00pm - 1:00pm,

这正是我想要的。

现在在服务器端,我使用split()将其解析为数组。

//parse string to array
var hours = req.body.hours.split(",");

//remove last element because it's ""
hours.splice(hours.length-1, 1);

console.log(hours[0]);

现在,当我记录这个时,我得到以下"11:00am - 12:00pm(开头的引文,但仅适用于数组中的第一个元素)

为什么会这样?

0 个答案:

没有答案