减少js里面if-else的数量

使用if-else
statusFilter: function (value) {
    if (value == "0") {
        return "分析中";
    } else if (value == "1") {
        return "分析成功";
    } else if (value == "-1") {
        return "分析失败";
    } else {
        return '待分析';
    }
}
  1. 使用switch代替if-else
    statusFilter: function (value) {
        switch (value) {
            case "0":
                return "分析中";
            case "1":
                return "分析成功";
            case "-1":
                return "分析失败";
            default:
                return '待分析';
        }
    }
  2. 使用map的特性(hash 表)
    statusFilter: function (value) {
        var status = {
            "0": "分析中",
            "1": "分析成功",
            "-1": "分析失败",
        }[value]
        status = status? status: "待分析"
        return status
    }
    
  3. 三目运算符
    alert(name==="bob"?'bob':'alie')
     
  4. map代替多组if
    const a = '23333';
    
    if (a === '23333' || a === '33333' || a === '43333' || a === '53333') {
        console.log(1);
    }
    var a = ["23333","33333","43333","53333"];
    if(a.includs("23333")){
        console.log(1)
    }
    if(a.indexOf("23333")>-1){
        console.log(1)
    }

     

  5. 复杂的判断
    const state = 'a';
    const author = 'me';
    
    function add(param) {
        console.log(param);
    }
    
    function go(param) {
        console.warn(param);
    }
    
    if (author === 'me') {
        if (state === 'a') {
            add('state1');
            go('state1')
        } else if (state === 'b') {
            add('state2');
            go('state2')
        } else if (state === 'c') {
            add('state3');
            go('state3')
        } else if (state === 'd') {
            add('state4');
            go('state4')
        } else if (state === 'e') {
            add('state5');
            go('state5')
        }
    } else if (author === 'you') {
        if (state === 'a') {
            add('state11');
            go('state11')
        } else if (state === 'b') {
            add('state22');
            go('state22')
        } else if (state === 'c') {
            add('state33');
            go('state33')
        } else if (state === 'd') {
            add('state44');
            go('state44')
        } else if (state === 'e') {
            add('state55');
            go('state55')
        }
    }
    const state = 'a';
    const author = 'me';
    
    function add(param) {
        console.log(param);
    }
    
    function go(param) {
        console.warn(param);
    }
    
    var map = new Map([
        [^/me_a$/,"state1"],
        [^/me_b|me_c|me_d|me_e$/,"state2"],
        ["you_a","state11"],
        ["you_b","state22"],
        ["you_c","state33"],
        ["you_d","state44"],
        ["you_e","state55"]
    ])
    
    var item = [...map].filter((key, value)=>{
        key.test(`${key}_${value}`)
    })
    var (key, value) = item[0]
    add(value);
    go(value)

     

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值