1

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?

2 Answers 2

4

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]
}
Sign up to request clarification or add additional context in comments.

Comments

0

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.