JavaScript Array JSON.parse (string)

192 views Asked by At

I am using eCharts (a JavaScript charting library) and there is something that's doing my head in. The examples use the following code for markLine (and it works as expected)

markLine : {
    symbol: 'none',
    tooltip: {show: false},
    itemStyle:{
        normal:{
            lineStyle:{
                type: 'solid',
                color: '#CCCCCC'
            },
            tooltip:{
                show: false
            }
        }
    },
    data: [ [{ "xAxis" : 250, "yAxis" : 0 }, {"xAxis": 250, "yAxis" : 250 }] ]
}

I need to be able to get the data part as a JSON string, but I cannot get it to work.
Note: For simplicity, I have the same JSON information I receive as a string called arrayString:

markLine : {
    symbol: 'none',
    tooltip: {show: false},
    itemStyle:{
        normal:{
            lineStyle:{
                type: 'solid',
                color: '#CCCCCC'
            },
            tooltip:{
                show: false
            }
        }
    },
    data: (function (){
        var res = [];
        var arrayString = "";

        arrayString = '[{ "xAxis" : 250, "yAxis" : 0 }, {"xAxis": 250, "yAxis" : 250 }]';
        res = JSON.parse(arrayString);

        return res;
    })()
}

When I run the JSON.parse code the chart doesn't display, but if I console.log the value of 'res' the array appears to be created correctly.

Is anyone able to assist me with resolving this?

References: Both of these examples use markLine:
http://echarts.baidu.com/echarts2/doc/example/line1.html#-en
http://echarts.baidu.com/echarts2/doc/example/bar13.html#-en

1

There are 1 answers

2
Nick On BEST ANSWER

Should you be adding to your res array rather than overwiting it?

res.push(JSON.parse(arrayString));