如何在 reactjs 请求中实现条件?
How to implement the condition in reactjs request?
我之前有过这样的请求:
let data = {
chartLibraryType : this.state.chartCategory == 'highChart'? "HIGH_CHARTS" : "C3",
};
现在我需要用 3 来实现。除了 highcharts 和 c3,我还需要通过 d3。
let data = {
chartLibraryType : if(this.state.chartCategory == 'highChart'){
"HIGH_CHARTS"
}else if(this.state.chartCategory == 'c3Chart'){
"C3"
}else if(this.state.chartCategory == 'D3Chart'){
"D3"
},
};
当我们有2个以上的时候,是这样实现的吗?
首先,您可以创建一个dictionary
。
let dictionary={
"highChart" : "HIGH_CHARTS",
"c3Chart" : "C3",
"D3Chart" : "D3"
}
然后您必须使用 bracket
符号将 state 作为 dictionary 的键传递。
let data = {
chartLibraryType : dictionary[this.state.chartCategory]
}
你可以使用像 si 这样的开关:
let data = { chartLibraryType: '' }
switch (this.state.chartCategory) {
case 'highChart':
data.chartLibraryType = 'HIGH_CHARTS'
break
case 'c3Chart':
data.chartLibraryType = 'C3'
break
case 'D3Chart':
data.chartLibraryType = 'D3'
break
// Add as much `case` as you wish without forgetting the break clause
// If you'd like to fallback to a default value
default:
break
}
也请大家看看这本大合集"You don't know JS"
我之前有过这样的请求:
let data = {
chartLibraryType : this.state.chartCategory == 'highChart'? "HIGH_CHARTS" : "C3",
};
现在我需要用 3 来实现。除了 highcharts 和 c3,我还需要通过 d3。
let data = {
chartLibraryType : if(this.state.chartCategory == 'highChart'){
"HIGH_CHARTS"
}else if(this.state.chartCategory == 'c3Chart'){
"C3"
}else if(this.state.chartCategory == 'D3Chart'){
"D3"
},
};
当我们有2个以上的时候,是这样实现的吗?
首先,您可以创建一个dictionary
。
let dictionary={
"highChart" : "HIGH_CHARTS",
"c3Chart" : "C3",
"D3Chart" : "D3"
}
然后您必须使用 bracket
符号将 state 作为 dictionary 的键传递。
let data = {
chartLibraryType : dictionary[this.state.chartCategory]
}
你可以使用像 si 这样的开关:
let data = { chartLibraryType: '' }
switch (this.state.chartCategory) {
case 'highChart':
data.chartLibraryType = 'HIGH_CHARTS'
break
case 'c3Chart':
data.chartLibraryType = 'C3'
break
case 'D3Chart':
data.chartLibraryType = 'D3'
break
// Add as much `case` as you wish without forgetting the break clause
// If you'd like to fallback to a default value
default:
break
}
也请大家看看这本大合集"You don't know JS"