条件判断的性能优化
if(){
}else if(){
}else
直接略过,不建议。。。
switch (this.currentWrite) {
case '项目经理初审':
this.form.pmFirstApprovalPersion = res.data.name
break;
case '执行部门审核1':
this.form.execDeptApprovePerson1 = res.data.name
break;
case '总经理审批':
this.form.gmApprovePerson = res.data.name
break;
case '远程服务-预判':
this.form.remoteFieldServiceExecutor = res.data.name
break;
case '执行部门审核2':
this.form.execDeptSecondServicePerson2 = res.data.name
break;
case '现场执行2':
this.form.fieldExecContactPerson2 = res.data.name
break;
case '执行部门审核3':
this.form.execDeptApprovalPerson3 = res.data.name
break;
case '申请人确认':
this.form.applyerConfirmPerson = res.data.name
break;
case '备案':
this.form.filingPerson = res.data.name
break;
default:
this.form.filingPerson2 = res.data.name
break;
}
虽然用switch性能比较好,但是明显很烦,我们可以用数组的形式来优化
const obj = [
'项目经理初审',
'执行部门审核1',
'总经理审批':2,
'远程服务-预判',
'执行部门审核2',
'现场执行2',
'执行部门审核3',
'申请人确认',
'备案'
]
const arr = [
this.form.pmFirstApprovalPersion,
this.form.execDeptApprovePerson1,
this.form.gmApprovePerson,
this.form.remoteFieldServiceExecutor,
this.form.execDeptSecondServicePerson2,
this.form.fieldExecContactPerson2,
this.form.execDeptApprovalPerson3,
this.form.applyerConfirmPerson,
this.form.filingPerson,
this.form.filingPerson2,
]
arr[obj.indexOf([this.currentWrite])+1 ?
obj.indexOf([this.currentWrite]) : arr.length-1
] = res.data.name