javascript 如何在不改变循环结构的情况下将字符串添加到 for 循环中的字符串变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9638688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:18:27  来源:igfitidea点击:

How to prepend a string to a string variable in a for loop without changing the loop structure?

javascriptjquery

提问by some_bloody_fool

If i have a for loop like this:

如果我有这样的 for 循环:

var rows;
var len = z[0].length;
for ( var i = len; i--; ) {
     rows += "<tr><td>" + z[0].Marketer + "</td><td>";
 }

How can i prepend instead of append the current row to this string WithOUT changing the for loop structure?

如何在不更改 for 循环结构的情况下将当前行添加到该字符串中,而不是附加到该字符串中?

var rows;
var len = z[0].length;
for ( var i = len; i--; ) {
     rows (prepend) "<tr><td>" + z[0].Marketer + "</td><td>";
 }

回答by talnicolas

Like this:

像这样:

rows = "<tr><td>" + z[0].Marketer + "</td><td>" + rows;