http://t.csdnimg.cn/j0gkOhttp://t.csdnimg.cn/j0gkO
一、各种循环方法如何跳出整个循环?
对于forEach()方法,目前似乎没有比较优雅的跳出整个循环的方法,如果你实在要用forEach()方法并且需要在某种条件下跳出整个循环提高遍历效率,可以考虑使用抛出异常的方式:trow
比如:
let arr=[1,2,3,4]
arr.forEach(el=>{
if(el==3){
trow "结束"
}
})
http://t.csdnimg.cn/j0gkOhttp://t.csdnimg.cn/j0gkO
对于forEach()方法,目前似乎没有比较优雅的跳出整个循环的方法,如果你实在要用forEach()方法并且需要在某种条件下跳出整个循环提高遍历效率,可以考虑使用抛出异常的方式:trow
比如:
let arr=[1,2,3,4]
arr.forEach(el=>{
if(el==3){
trow "结束"
}
})