我需要一些幫助,通過從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);
}
看看underscore.js,它有很多有用的實用函數集合和數組 –
我試過以下; – stevepop
如果你看看這行''類別:['10月27日','10月28日']你會看到你有一個sintax錯誤 –