码迷,mamicode.com
首页 > 其他好文 > 详细

foreach循环的跳出

时间:2019-11-22 19:36:12      阅读:57      评论:0      收藏:0      [点我收藏+]

标签:message   index   fun   not   遇到   ret   mes   each   top   

由于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循环的跳出

标签:message   index   fun   not   遇到   ret   mes   each   top   

原文地址:https://www.cnblogs.com/shenwh/p/11913294.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!