Reputation: 37
I have request like this previously:
let data = {
chartLibraryType : this.state.chartCategory == 'highChart'? "HIGH_CHARTS" : "C3",
};
Now I need to implement with 3. Along with highcharts and c3 I need to pass d3 also.
let data = {
chartLibraryType : if(this.state.chartCategory == 'highChart'){
"HIGH_CHARTS"
}else if(this.state.chartCategory == 'c3Chart'){
"C3"
}else if(this.state.chartCategory == 'D3Chart'){
"D3"
},
};
Is this the way to implement when we have more than 2?
Upvotes: 1
Views: 57
Reputation: 3487
You could use a switch like 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
}
I also invite you to take a look at this great collection of books "You don't know JS"
Upvotes: 0
Reputation: 48367
First of all, you can create a dictionary
.
let dictionary={
"highChart" : "HIGH_CHARTS",
"c3Chart" : "C3",
"D3Chart" : "D3"
}
Then you have to pass state as a key for dictionary using bracket
notation.
let data = {
chartLibraryType : dictionary[this.state.chartCategory]
}
Upvotes: 4