Reputation: 1649
I am trying to create a data object that looks like this:
var data = [{"label":"Category A", "value":20},
{"label":"Category B", "value":50},
{"label":"Category C", "value":30}];
I have a loop so far (below) that builds it as a string, but I am wondering if there is a better way to build this using some javascript objects:
for (i = 0; i < doughnutData.length; i += 3) {
if (doughnutData[i] != "" && (i != doughnutData.length - 1)) {
var dataValue = parseInt(doughnutData[i + 1], 10);
chartData.push('{"label":"' + doughnutData[i] + '", "value":' + dataValue + '}');
}
}
Upvotes: 2
Views: 58
Reputation: 104775
You've actually done a bit more work pushing a stringified version - you can easily create an object:
chartData.push({label: doughnutData[i], value: dataValue})
Upvotes: 4