我想做这样的事情:
for (var i=1; i<=10; i++) {
document.write(i + ",");
}
它显示的结果如下:
1,2,3,4,5,6,7,8,9,10,
但是我想去掉最后一个“,”,结果应该是这样的:
1,2,3,4,5,6,7,8,9,10
最佳答案
您应该使用 .join
代替:
var txt = []; //create an empty array
for (var i = 1; i <= 10; i++) {
txt.push(i); //push values into array
}
console.log(txt.join(",")); //join all the value with ","
关于javascript - JavaScript 中循环的最后一次迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15676819/