Saturday, 15 January 2011

javascript - trying to create a new json object foreach item in array -



javascript - trying to create a new json object foreach item in array -

i have big json files structure, lot more charts in each one:

{ "name": "test", "charts": [ { "chartname": "idle times", "disk": 0, "opcode": "read", "xaxis": [0,100,200,300,400,500], "yaxis": [337487,8565,11419,9704,7598] }, { "chartname": "idle times", "disk": 0, "opcode": "read", "xaxis": [0,100,200,300,400,500], "yaxis": [337487,8565,11419,9704,7598] }] }

the problem chart data, x-y data, in arrays , the javascript chart tool i'm using needs json this:

{"chartname":"idle times", "disk":0, "opcode":"read","xaxis":0, "yaxis":1234}, {"chartname":"idle times", "disk":0, "opcode":"read","xaxis":100, "yaxis":1234}, {"chartname":"idle times", "disk":0, "opcode":"read","xaxis":200, "yaxis":1234}, {"chartname":"idle times", "disk":0, "opcode":"read","xaxis":300, "yaxis":1234}, {"chartname":"idle times", "disk":0, "opcode":"read","xaxis":400, "yaxis":1234}, {"chartname":"idle times", "disk":0, "opcode":"read","xaxis":500, "yaxis":1234}, {"chartname":"idle times", "disk":0, "opcode":"read","xaxis":600, "yaxis":1234}

my app using angularjs, jslinq, d3js, , dimplejs currently. if there way accomplish these tools ideal. i'm open open source library, i'm hoping don't have read in whole json file , right pile of complicated loops building new json hand line line.

i'm open switching chart tool if there read json info way have now, dimplejs pretty easy utilize , looks sharp.

thanks in advance help!

so, if understood correctly, want expand each x , y coordinates separate points? seek this:

var source = { "name": "test", "charts": [ { "chartname": "idle times", "disk": 0, "opcode": "read", "xaxis": [0,100,200,300,400,500], "yaxis": [337487,8565,11419,9704,7598] }, { "chartname": "idle times", "disk": 0, "opcode": "read", "xaxis": [0,100,200,300,400,500], "yaxis": [337487,8565,11419,9704,7598] }] }; var info = source.charts.reduce(function(prev, now) { var target = []; for(var = 0, len = now.yaxis.length; < len; i++) { target.push({ "chartname": now.chartname, "disk": now.disk, "opcode": now.opcode, "xaxis": now.xaxis[i], "yaxis": now.yaxis[i] }) } homecoming prev.concat(target) }, [])

this should work, long have equal number of points.

javascript arrays json charts

No comments:

Post a Comment