优化多if和else语句
可以利用对象来进行映射
比如
let type = this.radio_value
if (type === 'whole') {
this.time_value = []
} else if (type === 'today') {
this.time_value = [this.getDay(0, '-'), this.getDay(1, '-')]
} else if (type === 'yesterday') {
this.time_value = [this.getDay(-1, '-'), this.getDay(0, '-')]
} else if (type === 'last_seven_days') {
this.time_value = [this.getDay(-7, '-'), this.getDay(0, '-')]
} else if (type === 'last_thirty_days') {
this.time_value = [this.getDay(-30, '-'), this.getDay(0, '-')]
}
可以优化成
let type = this.radio_value
let map = {
'whole':()=>this.time_value = [],
'today':()=>this.time_value = [this.getDay(0, '-'), this.getDay(1, '-')],
'yesterday':()=>this.time_value = [this.getDay(-1, '-'), this.getDay(0, '-')],
'last_seven_days':()=>this.time_value = [this.getDay(-7, '-'), this.getDay(0, '-')],
'last_thirty_days':()=>this.time_value = [this.getDay(-30, '-'), this.getDay(0, '-')],
}
if(map[type]){
map[type]()
}