zoukankan      html  css  js  c++  java
  • js中跳出循环的方式

    • for循环
    1. 跳出本次循环continue,继续下次循环
    var arr = [1,2,3,4,5,6,7,8]
    for(var i=0, len = arr.length ; i< len ; i++){
        if(i == 2){ 
            continue;
        }
       console.log(i);
    }  
     //0,1,3,4,5,6,7
    
    
    1. 跳出整个循环break
    for(var i=0, len = arr.length ; i< len ; i++){
        if(i == 2){
            break;
        }
        console.log(i);    
    }
    
    // 0,1
    
    • for-in 循环

    退出方式同for循环

    • jq的$.each循环
    1. 退出当前循环 return true
    $.each(arr,function(index,oo){
       if(index == 2){
           return true;
       }
       console.log(oo);
    })
    // 1,2,4,5,6,7,8
    
    1. 退出整个循环return false
    $.each(arr,function(index,oo){
        if(index == 2){
            return false;
        }
        console.log(oo);
    });
     
    //  1,2,3
    
    • forEach循环
    1. 退出当前循环
    arr.forEach(function(oo,index){
        if(index == 2){
            return;
            //return false;    //效果同上
           // return true;    //效果同上
        }
        console.log(oo);
    });
    // 1,2,4,5,6,7,8
    
    
    1. 退出整个forEach循环:抛异常
    try{
        arr.forEach(function(oo,index){
            if(index == 2){
                 throw 'jumpout';
            }
            console.log(oo);
        });
    }catch(e){
    }
     
    // 1,2
     
    
  • 相关阅读:
    安装selenium
    虚拟机安装Linux系统
    Pycharm安装+python安装+环境配置
    shell命令
    单例模式
    装饰者模式
    AtomicInteger的CAS原理
    J.U.C总览图
    锁机制(四)
    锁机制(三)
  • 原文地址:https://www.cnblogs.com/justyouadmin/p/13276463.html
Copyright © 2011-2022 走看看