我需要一些帮助来遍历我从 API 作为 JSON 接收的对象,并为图表库组装另一个对象。
这是对象的示例:
var channel_data = [
{
"27th Oct 12": {
"xaxis": "27th Oct 12",
"channels": {
"Create and Craft": {
"views": 13064,
"bandwidth": 623136899214,
"name": "Create and Craft"
},
"5 USA": {
"views": 394,
"bandwidth": 17889861768,
"name": "5 USA"
}
},
"totalViews": 25470,
"totalBandwidth": 923866774348
},
"28th Oct 12": {
"xaxis": "28th Oct 12",
"channels": {
"Create and Craft": {
"views": 15298,
"bandwidth": 860108829822,
"name": "Create and Craft"
},
"5 USA": {
"views": 500,
"bandwidth": 28770314737,
"name": "5 USA"
}
},
"totalViews": 29377,
"totalBandwidth": 1248778079465
}
}
]
我需要遍历这些数据并构建以下对象数组以用于 Highcharts 渲染。类别将是日期,数据是每个频道的每个日期的视图。
categories: ['27th Oct 12", "28th Oct 12"]
series: [{
name: 'Create and Craft',
data: [13064, 15298]
}, {
name: '5 USA',
data: [394, 500]
}, {
}]
我尝试了以下但最终得到了错误的结构;
var dates = [], channels=[], views = [];
for (var gdata in channel_data) {
dates.push(channel_data[gdata].xaxis);
for (var channel in channel_data[gdata].channels){
channel_name = channel_data[gdata].channels[channel].name;
channel_views = channel_data[gdata].channels[channel].views;
channel_data = {"name" :channel_name,"views" : channel_views}
channels.push(channel_data);
}
views.push(model.attributes[gdata].channels.name.views);
}