foreach跳出循环

var array = [1,2,3,4,5];
try {
    // 执行到第3次,结束循环
    array.forEach((item, index)=> {
        // debugger
        if (item == 3) {
            throw new Error("跳出循环");
        }
        console.log(item);  //1,2
    });
}
catch (e) { //  抛出异常
    console.error(e);
};
// 下面的代码不影响继续执行
console.log("继续执行");```
posted @ 2021-06-03 11:28  icon-icon  阅读(96)  评论(0编辑  收藏  举报