0

How to convert string to int from json

     if(this.state.chartCategory == 'D3Chart'){
      console.log("chartdata for d3 charts", this.state.d3line.chartData);
      var a = parseInt(this.state.d3line.chartData)
      console.log("a", a);
        switch(this.state.chartType){
          case 'area': return <div  style={{width: 900, height:300}}><Areagraph  data = {this.state.d3line.chartData} vars = {{x: this.state.d3line.x, y: this.state.d3line.y}} dispatch = {this.showFilter} title = {this.state.data.chartName}/></div>;
          case 'pie': return  <div style={{width: 900, height:500}}><Piegraph  data = {this.state.d3line.chartData} vars = {{x: this.state.d3line.x, y: this.state.d3line.y}} dispatch = {this.showFilter} title = {this.state.data.chartName}/></div>;
          case 'bar': return  <div  style={{width: 900, height:300}}><Bargraph  data = {this.state.d3line.chartData} vars = {{x: this.state.d3line.x, y: this.state.d3line.y}} dispatch = {this.showFilter} title = {this.state.data.chartName}/></div>;
          case 'line' : return <div  style={{width: 900, height:300}}><Linegraph  data = {this.state.d3line.chartData} vars = {{x:this.state.d3line.x, y: this.state.d3line.y, group: this.state.d3line.x}} dispatch = {this.showFilter} title = {this.state.data.chartName}/></div>;
          default: return null;
       }   

    }

I am getting a as NAN i want to check if the value is integer convert string to integer else it should remains same

Thanks in advance

6
  • Where exactly in your code do you want to convert what value? Commented Nov 24, 2017 at 10:27
  • Please, could you clarify your question? Can you show us some example that you have tried? Commented Nov 24, 2017 at 10:27
  • 1
    var a = parseInt(this.state.d3line.chartData) you cannot parse a complex object straight to a single integer, that's why you're getting NaN Commented Nov 24, 2017 at 10:28
  • 1
    Please add exactly what do you want to convert. Please show exactly what data is returned by "this.state.d3line.chartData" Commented Nov 24, 2017 at 10:29
  • also, please do not post pictures of code or results, just add them as snippets in your questions. Commented Nov 24, 2017 at 10:32

1 Answer 1

0

i think this is what you are after.

(Admittedly there a prettier ways to do this in ES6)

if(this.state.chartCategory == 'D3Chart'){

    //convert all UNIT_PRICE values in this.state.d3line.chartData to number
    for(var i=0; i < this.state.d3line.chartData.length; i++)
    {
        this.state.d3line.chartData[i].UNIT_PRICE = parseInt(this.state.d3line.chartData[i].UNIT_PRICE);
    }

    switch(this.state.chartType){
      case 'area': return <div  style={{width: 900, height:300}}><Areagraph  data = {this.state.d3line.chartData} vars = {{x: this.state.d3line.x, y: this.state.d3line.y}} dispatch = {this.showFilter} title = {this.state.data.chartName}/></div>;
      case 'pie': return  <div style={{width: 900, height:500}}><Piegraph  data = {this.state.d3line.chartData} vars = {{x: this.state.d3line.x, y: this.state.d3line.y}} dispatch = {this.showFilter} title = {this.state.data.chartName}/></div>;
      case 'bar': return  <div  style={{width: 900, height:300}}><Bargraph  data = {this.state.d3line.chartData} vars = {{x: this.state.d3line.x, y: this.state.d3line.y}} dispatch = {this.showFilter} title = {this.state.data.chartName}/></div>;
      /*case 'column': return  <div className = "graphContainer" style={{width: 900, height:300, overflow: "hidden", resize: "both", paddingBottom: "10px", border: "1px solid black"}}><Columngraph  data = {this.state.d3line.chartData} vars = {{x: this.state.d3line.x, y: this.state.d3line.y}} dispatch = {this.showFilter} title = {this.state.data.chartName}/></div>;*/
      case 'line' : return <div  style={{width: 900, height:300}}><Linegraph  data = {this.state.d3line.chartData} vars = {{x:this.state.d3line.x, y: this.state.d3line.y, group: this.state.d3line.x}} dispatch = {this.showFilter} title = {this.state.data.chartName}/></div>;
      default: return null;
   }   

}
Sign up to request clarification or add additional context in comments.

1 Comment

instead of unit price if i put this.state.d3line.y showing state error

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.