这段 if...else 有优雅的写法吗?

查看 305|回复 22
z1829909   
举手提问, 这里为啥需要转成数字传入 fetchData, 为什么不在这一层直接转为查询条件传入 fetchData.
因为我觉得 sortType 在 fetchData 中又要被拆开解析, 看代码的人又要去记 sortType 的意思, 多一层开销
foolnius   
@foolnius #17
"thisYearIncome: 3, //注释" => "thisYearIncome: 2, //注释"
makejohn2015   
function onSortChange({ order, prop }) {
const sortMap = {
thisYearIncome: { ascending: 2, descending: 1 },
lastYearIncome: { ascending: 4, descending: 3 },
rate: { ascending: 6, descending: 5 }
};
const sortType = sortMap[prop] ? sortMap[prop][order] : 0;
this.fetchData(sortType);
}
您需要登录后才可以回帖 登录 | 立即注册

返回顶部