JavaScript split gives array size one instead of zero for empty string

前端 未结 3 1576
礼貌的吻别
礼貌的吻别 2021-01-25 23:38

I want to split a string by comma to get an array in Node.js.

exports.test = function(rq, rs){

   var mailList = \"sharan@test.com,pradeep@test.com\";
   var ar         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-26 00:19

    One way to do is to check whether the first value of the split array is empty or not

    var arrayList = mailList.split(",");
    if(arrayList[0] != ""){
      arrayLength = arrayList.length;
    }else{
      arrayLength = 0;
    }
    

提交回复
热议问题