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;
}
}
我得到一个作为NAN我想检查值是否为整数将字符串转换为整数否则它应该保持相同
提前致谢
答案 0 :(得分:0)
我认为这就是你所追求的目标。
(无可否认,在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;
}
}