var order="xxx",prop="rate" var sortType=( /**/ ({descending: /*----*/ {thisYearIncome:1,lastYearIncome:3,rate:5} /**/ })[order] || /*----*/ {thisYearIncome:2,lastYearIncome:4,rate:6} )[prop]||0;
先定义枚举吧,不然时间久了怎么解释这些 1 到 6 ··· enum SortType { THIS_YEAR_INCOME_DESC = 1, THIS_YEAR_INCOME_ASC = 2, LAST_YEAR_INCOME_DESC = 3, LAST_YEAR_INCOME_ASC = 4, RATE_DESC = 5, RATE_ASC = 6 } ···
function onSortChange({ order, prop }) { const sortMap = { 'descending': { 'thisYearIncome': 1, 'lastYearIncome': 3, 'rate': 5 }, 'ascending': { 'thisYearIncome': 2, 'lastYearIncome': 4, 'rate': 6 } }; const sortType = sortMap[order]?.[prop] || 0; this.fetchData(sortType); }
function onSortChange({ order, prop }) { const sortTypes = { thisYearIncome: order === 'descending' ? 1 : 2, lastYearIncome: order === 'descending' ? 3 : 4, rate: order === 'descending' ? 5 : 6, }; this.fetchData(sortTypes[prop] || 0); }
const defineObject = { descending_thisYearIncome: 1, //注释 thisYearIncome: 3, //注释 descending_lastYearIncome: 3, //注释 lastYearIncome: 4, //注释 descending_rate: 5, //注释 rate: 6, //注释 }; function onSortChange({ order, prop }) { const key = `${order === "descending" ? `${order}_` : ""}${prop}`; this.fetchData(defineObject[key] ?? 0); }
function onSortChange({ order, prop }) { const l=["thisYearIncome", "lastYearIncome", "rate"]; const sortType = order === "descending" ? l.indexOf(prop)*2+1 : l.indexOf(prop)*2+2; this.fetchData(sortType) }