如何在 for-in 中跳到 javascript 中的下一个,里面有一段时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15034763/
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
How to skip to next in javascript in a for-in with a while inside?
提问by Ram Iyer
I have a short javascript code where I need to skip to next in the for loop....see below:
我有一个简短的 javascript 代码,我需要跳到 for 循环中的下一个......见下文:
var y = new Array ('1', '2', '3', '4');
for (var x in y) {
callFunctionOne(y[x]);
while (condition){
condition = callFunctionTwo(y[x]);
//now want to move to the next item so
// invoke callFunctionTwo() again...
}
}
Wanted to keep it simple so syntax may be error free.
想要保持简单,以便语法可能没有错误。
回答by Blender
Don't iterate over arrays using for...in
. That syntax is for iterating over the properties of an object, which isn't what you're after.
不要使用for...in
. 该语法用于迭代对象的属性,这不是您所追求的。
As for your actual question, you can use the continue
:
至于您的实际问题,您可以使用continue
:
var y = [1, 2, 3, 4];
for (var i = 0; i < y.length; i++) {
if (y[i] == 2) {
continue;
}
console.log(y[i]);
}
This will print:
这将打印:
1
3
4
Actually, it looks like you want to break out of the while
loop. You can use break
for that:
实际上,看起来您想跳出while
循环。你可以使用break
:
while (condition){
condition = callFunctionTwo(y[x]);
break;
}
Take a look at do...while
loops as well.
也看看do...while
循环。