How to prevent values from being converted to strings in javascript?

前端 未结 5 1774
灰色年华
灰色年华 2021-01-20 16:58
    var str;
    var displayedNum;
    for (i in imgURLArray){
        str = \"
  • \" + \"\"+ (1+i) + \"\" + \"
  • \
    5条回答
    •  甜味超标
      2021-01-20 17:19

      The reason is due to your loop:

      for (i in imgURLArray){
      

      This iterates over all the property names of imgURLArray as strings. So you will need to use Number() to convert i to an integer:

          str = "
    • " + ""+ (1+Number(i)) + "" + "
    • ";

    提交回复
    热议问题