I have the following json array
{"label":[{"name":"Government Schools",
"datapoints":[{"year":"2007-2008","total":"1300"},
{"year":"2008-2009","total":"1280"},
{"year":"2009-2010","total":"1100"},
{"year":"2010-2011","total":"1400"}]
},
{"name":"Total Schools",
"datapoints":[{"year":"2007-2008","total":"900"},
{"year":"2008-2009","total":"1000"},
{"year":"2009-2010","total":"1300"},
{"year":"2010-2011","total":"999"}]
}]
}
I am trying to sort the datapoints into an array with the following format
var linedataline = [{dataPoints : [{ x: 1,
y: 1300,
label: "2007-2008"
}, {
x: 2,
y: 1280,
label: "2008-2009"
}, {
x: 3,
y: 1100,
label: "2009-2010"
}, {
x: 4,
y: 1400,
label: "2010-2011"
}]
},
{dataPoints : [{ x: 1,
y: 900,
label: "2007-2008"
}, {
x: 2,
y: 1000,
label: "2008-2009"
}, {
x: 3,
y: 1300,
label: "2009-2010"
}, {
x: 4,
y: 999,
label: "2010-2011"
}]
}];
I have used the following javascript
for(var i=0; i<data.label.length;i++){
datapoints.length = 0;
for(var j=0;j<data.label[i].datapoints.length;j++){
datapoints.push({
x: j+1,
y: data.label[i].datapoints[j].total,
label: data.label[i].datapoints[j].year
})
}
linedataline.push({
dataPoints: datapoints
})
}
where "data" is the parsed JSON. The problem is that linedataline does contain two arrays as required, but both print the second JSON array and not the first one. If I do not empty the datapoints array, it returns both the arrays but linedataline has array length = 8 and it does not separate the two arrays. What is the problem here?