for循环-如何在内部有while的for in中跳过javascript中的next

for loop - How to skip to next in javascript in a for-in with a while inside?

本文关键字:for javascript next 中的 in while 循环 在内部      更新时间:2023-09-26

我有一个简短的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...
   }
}

希望保持简单,这样语法就不会出错。

不要使用for...in迭代数组。该语法用于迭代对象的属性,而这不是您想要的。

至于您的实际问题,您可以使用continue:

var y = [1, 2, 3, 4];
for (var i = 0; i < y.length; i++) {
    if (y[i] == 2) {
        continue;
    }
    console.log(y[i]);
}

这将打印:

1
3
4

实际上,看起来您想要突破while循环。你可以使用break

while (condition){
    condition = callFunctionTwo(y[x]);
    break;
}

还可以看看do...while循环。