JS中arr.forEach()如何跳出循环

第一种方法

try{
        var array = ["first","second","third","fourth"];
        array.forEach(function(item,index){
                if(item == "third"){
                        var a = aaaa;// first second 后就报错,就跳出循环了
                        throw new Error("ending");//报错,就跳出循环
                }else{
                        log(item);
                }
        })
}catch(e){
        if(e.message == "ending"){
                log("结束了") ;
        }else{
                log(e.message);
        }

其它方法详见
https://blog.csdn.net/guxin_duyin/article/details/82980062

你可能感兴趣的:(JS中arr.forEach()如何跳出循环)