foreach循环的跳出

Posted shenwh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了foreach循环的跳出相关的知识,希望对你有一定的参考价值。

由于foreach循环中不像for循环可以直接通过return或break来终止当前循环,不过这里可以借助try...catch。。。来完成
var arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14];
try{ arr.forEach(function(el,index){   if (el==6) {   console.log("try中遇到20,能退出吗?");//   foreach.break=new Error("StopIteration");   }else{   console.log(el);   }   }); }catch(e){   console.log(e.message);   if(e.message==="foreach is not defined") {     console.log("跳出来了?");//     return;   }else throw e;
}

以上是关于foreach循环的跳出的主要内容,如果未能解决你的问题,请参考以下文章