从javascript的内部循环中跳出外部循环 [英] Breaking out of an outer loop from an inner loop in javascript

查看:85
本文介绍了从javascript的内部循环中跳出外部循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

while(valid){
   for(loop through associative array){
      if(!valid){
         break;
      }
   }
}

我试图找到一种从if语句中退出while循环的方法.到目前为止,最好的方法似乎是Javascript中不存在的goto方法.使if语句脱离嵌套在其中的两个循环中的最佳方法是什么?预先感谢您的帮助!

I have tried to find a way to break out of the while loop from the if statement. So far, the best method seems to be the goto method that is non-existant in Javascript. What is the best way to cause the if statement to break out of both of the loops it is nested in? Thanks in advance for the help!

推荐答案

根据您的条件,很容易将for循环的迭代器设置为会破坏它的东西,并将while条件设置为错误的.例如,

Depending on what your conditionals are, it should be easy to set the iterator of your for-loop to something that would break it, and set your while condition to false. For example,

while(someBoolean){
    for(var i = 0; i < size; i++){
        if(wantToBreak){
            i = size;
            someBoolean = false;
        }else{
            //Do Stuff
        }
    }
}

这篇关于从javascript的内部循环中跳出外部循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆