Javascript javascript退出for循环而不返回

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10471456/
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-08-24 01:28:15  来源:igfitidea点击:

javascript exiting for loop without returning

javascriptfor-loop

提问by frenchie

I have a for loop that I want to exit like this:

我有一个 for 循环,我想像这样退出:

function MyFunction() {
  for (var i = 0; i < SomeCondition; i++) {
     if (i === SomeOtherCondition) {
        // Do some work here.
        return false;
     }
  }
  // Execute the following code after breaking out of the for loop above.
  SomeOtherFunction();
}

The problem is that after the // Do some work here.statements executes, I want to exit the for loop but still want to execute the code below the whole for loop (everything below // Execute the following code after breaking out of the for loop above.).

问题是// Do some work here.语句执行后,我想退出 for 循环,但仍然想执行整个 for 循环下面的代码(下面的所有内容// Execute the following code after breaking out of the for loop above.)。

The return falsestatement does exit the for loop but it also exits the whole function. How do I fix this?

return false语句确实退出了 for 循环,但它也退出了整个函数。我该如何解决?

回答by SLaks

You're looking for the breakstatement.

您正在查找break语句

回答by Chris Gessler

Either use a break or continue statement

使用 break 或 continue 语句

function MyFunction() { 
  for (var i = 0; i < SomeCondition; i++) { 

     if (i === SomeOtherCondition) { 

        // Do some work here 
        break;
     } 
  } 

  SomeOtherFunction(); 
  SomeOtherFunction2(); 
} 

Or to continue processing items except for those in a condition

或继续处理条件中的项目以外的项目

function MyFunction() { 
  for (var i = 0; i < SomeCondition; i++) { 

     if (i != SomeOtherCondition) continue;

     // Do some work here 
  } 

  SomeOtherFunction(); 
  SomeOtherFunction2(); 
} 

回答by Spudley

Several people have offered breakas the solution, and it is indeed the best answer to the question.

几个人提供break了解决方案,这确实是问题的最佳答案。

However, just for completeness, I feel I should also add that the question could be answered while retaining the returnstatement, by wrapping the contents of the if()condition in a closure function:

但是,为了完整起见,我觉得我还应该补充一点return,通过将if()条件的内容包装在闭包函数中,可以在保留语句的同时回答问题:

function MyFunction() {

  for (var i = 0; i < SomeCondition; i++) {

     if (i === SomeOtherCondition) {
        function() {
           // Do some work here
           return false;
        }();
     }
  }

  SomeOtherFunction();
  SomeOtherFunction2();
}

As I say, breakis probably a better solution in this case, as it's the direct answer to the question and the closure does introduce some additional factors (such as changing the value of this, limiting the scope of variables introduced inside the function, etc). But it's worth offering as a solution, because it's a valuable technique to learn, if not necessarily to be used in this particular occasion then certainly for the future.

正如我所说,break在这种情况下可能是更好的解决方案,因为它是问题的直接答案,并且闭包确实引入了一些额外的因素(例如更改 的值this、限制在函数内部引入的变量的范围等)。但它作为一种解决方案值得提供,因为它是一种值得学习的技术,如果不一定要在这个特定场合使用,那么肯定会在未来使用。

回答by Erik H?ggqvist

Break - breaks the whole loop. Continue - skips a step in a loop. So it skips the code below continue;

Break - 中断整个循环。继续 - 在循环中跳过一个步骤。所以它跳过下面的代码继续;

回答by Ri?l

Would setting the i variable to the somecondition value be a good way?

将 i 变量设置为 somecondition 值是一个好方法吗?

for (var i=0; i<SomeCondition; i++) {

   if (data[i]===true) {
   //do stuff
   i=SomeCondition;
   }
}

回答by mariogarranz

OK maybe this is an old topic, but after reading all the answers I'm wondering why is it that nobody suggested using a while loop instead?

好吧,也许这是一个老话题,但在阅读了所有答案后,我想知道为什么没有人建议使用 while 循环?

I guess in JavaScript you can break a for loop (which you can not do in many other languages, or is considered a bad practice) but I would still use for loops only for the situations where you want to iterate the loop a fixed amount of times.

我想在 JavaScript 中你可以打破一个 for 循环(你不能在许多其他语言中这样做,或者被认为是一种不好的做法)但我仍然只会在你想要迭代循环固定数量的情况下使用 for 循环次。

This would be my suggestion:

这将是我的建议:

function MyFunction() {

  var i = 0,
      breakLoop = false;

  while (i < SomeCondition && !breakLoop) {

     if (i === SomeOtherCondition) {
        breakLoop = true;
     }

     i++;

  }

  SomeOtherFunction();
  SomeOtherFunction2();

}