js forEach(function(){ //跳出循环的})

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);
        }
}

Brief  summary:

        目前只有这种方法能break forEach循环,所以在这一点上是不方便的。

猜你喜欢

转载自blog.csdn.net/tangcc110/article/details/80534212