当前位置: 首页 > Web前端 > JavaScript

js停止forEach的方法

时间:2023-03-26 20:33:54 JavaScript

可以利用trycatch的异常行为巧妙的停止forEach遍历开发。当然,你不能在写采访时这样做。用trycatch包装forEach以停止try{[1,2,3,4,5,6].forEach(function(item,index){console.log(item);if(item===3){thrownew错误('块');}});}catch(error){console.log('error_blocked成功',error);}forEach包含trycatch无法成功//失败案例[1,2,3,4,5,6].forEach(function(item,index){if(item===3){try{thrownewError('stopfailed');}catch(error){console.log('error','error_stopfailed');}}});