Javascript how to show each element of array on a new line

前端 未结 3 1580
小鲜肉
小鲜肉 2021-02-02 15:21

I have a string build form comma separated values I use split to get each value and after that I want to show each value on a new line but what really happens is th

3条回答
  •  一个人的身影
    2021-02-02 15:54

    The for-loop is suspicious. Firstly, you do not process all items (the last one is missing, as @sarfraz pointed out). Sencondly you are returning the result (zzz) in the for-loop body:

    for (var i=0; i<=xxx; i++)
    {
      zzz[i] = zzz[i] + '
    '; return zzz; // for-loop will stop here! resulting in ["value1
    ", "Value2", etc...] }

    In Javscript you can simple "join" the array:

    return dates.split(',').join("
    ")

    Since you are simply replacing strings you could use the replace method:

    return dates.replace(",", "
    ");

提交回复
热议问题