1

如何将switch语句遍历对象优化的更精简

switch (this.active) {
    case 0:
      this.OrderState = 'Wait';
      break;
    case 1:
      this.OrderState = 'Sent';
      break;
    case 2:
      this.OrderState = 'Fail';
      break;
    default:
      break;
}
楠枫瑾 228
2019-01-04 提问
2 个回答
2

已采纳
const temp = ['wait', 'Sent', 'Fail'];
this.OrderState = temp[this.active]
1

2019年了,肯定玩点骚操作啊,用Map来表示映射关系。

let stateMap = new Map

stateMap
    .set(0, 'Wait')
    .set(1, 'Sent')
    .set(2, 'Fail')
    
this.OrderState = stateMap.get(this.active)

撰写答案

推广链接