嵌套条件if分区模板 (前端)


//适用场景 if(config1=='xxx'||config2=='xxx'||config3=='xxx')
//更改为 if(or())

export function or(){
    
    do{
       
        if(config1=='xxx'){
            console.log('满足条件一')
            break
        }
      
        if(config2=='xxx'){
             break
        }
      
        if(config3=='xxx'){
            break
        }
        //所有条件都不满足返回false
        console.log('所有条件都不满足')
        return false
    }
    while(false)
    //只要满足一个返回true
    return true
}

//适用场景 if(config1=='xxx'&&config2=='xxx'&&config3=='xxx')
//多if层级嵌套
// if(config1=='xxx'){
//     if(config2=='xxx'){
//         if(config3=='xxx'){

//         }
//     }
// }
//更改为 if(and())

export function and(){
    
    do{
       
        if(config1=='xxx'){

        }
        else{
            console.log('不满足条件一')
            break
        }
      
        if(config2=='xxx'){
        
        }
        else{
            console.log('不满足条件二')
            break
        }
      
        if(config3=='xxx'){
             
        }
        else{
            console.log('不满足条件三')
            break
        }
        //所有条件都满足返回true
        console.log()
        return false
    }
    while(false)
    //只要有一个不满足返回false
    return true
}

//组合拳 适用场景if((config1==1&&config2==2)||config3==3)只看最外层为or
// 更改为if(or())
const map={config1:1,config2:2,config:3}
export function or(map){
    
    do{
       
        if(and(map)){
            console.log('满足条件一')
            break
        }
      
        if(map.config3==3){
            break
        }
        //所有条件都不满足返回false
        console.log('所有条件都不满足')
        return false
    }
    while(false)
    //只要满足一个返回true
    return true
}

export function and(map){
    
    do{
       
        if(map.config1=='xxx'){

        }
        else{
            console.log('不满足条件一')
            break
        }
      
        if(map.config2=='xxx'){
        
        }
        else{
            console.log('不满足条件二')
            break
        }
   
        //所有条件都满足返回true
        return false
    }
    while(false)
    //只要有一个不满足返回false
    return true
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值