大串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 }

是不是精简了很多,逻辑也更明确,甚至运行起来都感觉效率高了一点,不知道这是不是所谓的映射呢

    推荐阅读