如何在javascript中转换while循环到for循环
How can I convert while loop to for loop in javascript?
我有问题,转换while循环到for循环。这将如何在一个循环格式,任何帮助将是非常感激。实际文件存储在github上> github link
//language
while ((m = regex.exec(str)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
m.forEach((match, groupIndex) => {
output = output+`{'n"Language": "${match}"'n`;
});
}
回想一下while
循环的标准形式:
while (test) {
body;
}
和for
环的标准形式:
for (initialization; test; update) {
body;
}
这是可能改变你的while
到for
,但它没有多大意义:
for (m = regex.exec(str); m !== null ; m = regex.exec(str)) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
m.forEach((match, groupIndex) => {
output = output + `{'n"Language": "${match}"'n`;
});
}
注意初始化和更新是相同的;重复代码。
交替:
for ( ; (m = regex.exec(str)) !== null ; ) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
m.forEach((match, groupIndex) => {
output = output + `{'n"Language": "${match}"'n`;
});
}
注意初始化和更新都是空的
相关文章:
- 基于两个条件退出While循环
- 希望日期开始结束于while循环中的一个房间id的一个数组
- 为什么我的While循环不起作用
- 使用while循环+break是一种可接受的方式;goto;手术的结束
- 如何将while循环中生成的值存储到变量中,以便以后在JQuery中使用
- jquery,while循环在后台运行,同时while循环
- jquery悬停和while循环
- Javascript:用while循环在给定的count参数中重复一个字符串
- 如何修复 while 循环
- Jquery扩展点的宽度,同时通过while循环递增
- 斐波那契序列Javascript做while循环
- 为什么这个while循环不能使用AND逻辑运算符
- bluebird promise的while()循环正确(无递归?)
- JavaScript while循环没有'不能在有条件的情况下工作
- While循环在JavaScript中永远运行
- 逻辑(JSint友好)等效于while循环中的此赋值
- Javascript:用if语句运行while循环,用子句随机生成内容
- 如何使javascript在SQL while循环中回显所有数据
- 使用ajax循环while中的多个表单
- JavaScript 中的无限循环 - while 循环