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

版权声明:所有原创文章未经本人同意不得随意转载,谢谢 https://blog.csdn.net/tangcc110/article/details/80534212
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循环,所以在这一点上是不方便的。

没有更多推荐了,返回首页