1. 程式人生 > 其它 >大串if else優化

大串if else優化

廢話不多說,上程式碼

原來的樣子:

// return回去的是定義好的陣列物件
function getColByType(type) {
    console.log(type, 'type---------------55');
    if (type == "jcxxList") {
        return jcxx
    } else if (type == 'tgxxList') {
        return tgxx
    } else if (type == 'sbzzList') {
        return sbzzList
    } else if (type == 'sbsjJyzbList') {
        return sbsjJyzbList
    } else if (type == 'jcxxFjkgList') {
        return jcxxFjkgList
    } else if (type == 'jcxxYlsfzzList') {
        return jcxxYlsfzzList
    } else if (type == 'jcxxLqqList') {
        return jcxxLqqList
    } else if (type == 'tgsDlList') {
        return tgsDlList
    } else if (type == 'jcxxByqyList') {
        return jcxxByqyList
    } else if (type == 'jcxxByqfznlList') {
        return jcxxByqfznlList
    } else if (type == 'jcxxZsspList') {
        return jcxxZsspList
    } else if (type == 'jcxxWxdgrspList') {
        return jcxxWxdgrspList
    } else if (type == 'jcxxWxdgrspList') {
        return jcxxWxdgrspList
    }
    else if (type == 'sbsjGgpList') {
        return sbsjGgpList
    } else if (type == 'sbsjZlpcnsList') {
        return sbsjZlpcnsList
    } else if (type == 'sbsjGlcList') {
        return sbsjGlcList
    } else if (type == 'sbsjDgcymList') {
        return sbsjDgcymList
    } else if (type == 'sbsjMfjList') {
        return sbsjMfjList
    } else if (type == 'sbsjCygList') {
        return sbsjCygList
    } else if (type == 'sbsjCygjnList') {
        return sbsjCygjnList
    } else if (type == 'sbsjYwjList') {
        return sbsjYwjList
    } else if (type == 'sbsjMwfxsqList') {
        return sbsjMwfxsqList
    } else if (type == 'sbsjQtjdqList') {
        return sbsjQtjdqList
    } else if (type == 'sbsjYzfjkgList') {
        return sbsjYzfjkgList
    } else if (type == 'sbsjWlcfjkgList') {
        return sbsjWlcfjkgList
    } else if (type == 'sbsjYlsfzzList') {
        return sbsjYlsfzzList
    } else if (type == 'sbsjYmwkqList') {
        return sbsjYmwkqList
    }
}

改造之後的樣子:

function getColByType(type) {
    console.log(type, 'type---------------55');
    let list = {
        "jcxxList": jcxx,
        'tgxxList': tgxx,
        'sbzzList': sbzzList,
        'sbsjJyzbList': sbsjJyzbList,
        'jcxxFjkgList': jcxxFjkgList,
        'jcxxYlsfzzList': jcxxYlsfzzList,
        'jcxxLqqList': jcxxLqqList,
        'tgsDlList': tgsDlList,
        'jcxxByqyList': jcxxByqyList,
        'jcxxByqfznlList': jcxxByqfznlList,
        'jcxxZsspList': jcxxZsspList,
        'jcxxWxdgrspList': jcxxWxdgrspList,
        'sbsjGgpList': sbsjGgpList,
        'sbsjZlpcnsList': sbsjZlpcnsList,
        'sbsjGlcList': sbsjGlcList,
        'sbsjDgcymList': sbsjDgcymList,
        'sbsjMfjList': sbsjMfjList,
        'sbsjCygList': sbsjCygList,
        'sbsjCygjnList': sbsjCygjnList,
        'sbsjYwjList': sbsjYwjList,
        'sbsjMwfxsqList': sbsjMwfxsqList,
        'sbsjQtjdqList': sbsjQtjdqList,
        'sbsjYzfjkgList': sbsjYzfjkgList,
        'sbsjWlcfjkgList': sbsjWlcfjkgList,
        'sbsjYlsfzzList': sbsjYlsfzzList,
        'sbsjYmwkqList': sbsjYmwkqList,
    }
    return list[type] ? list[type] : false
}
是不是精簡了很多,邏輯也更明確,甚至執行起來都感覺效率高了一點,不知道這是不是所謂的對映呢