我正在使用highcharts脚本创建图表,我创建了一个php脚本,我用ajax调用它。该脚本返回json数据,我需要以highcharts基本列格式重新格式化。
[{"count":"391","search_date":"2015-11-24","search_area":"lastni-pre"},
{"count":"63","search_date":"2015-11-24","search_area":"letalske"},
{"count":"501","search_date":"2015-11-24","search_area":"pocitnice"},
{"count":"114","search_date":"2015-11-24","search_area":"potovanja"},
{"count":"457","search_date":"2015-11-25","search_area":"lastni-pre"},
{"count":"103","search_date":"2015-11-25","search_area":"letalske"},
{"count":"509","search_date":"2015-11-25","search_area":"pocitnice"},
{"count":"84","search_date":"2015-11-25","search_area":"potovanja"},
{"count":"399","search_date":"2015-11-26","search_area":"lastni-pre"},
{"count":"106","search_date":"2015-11-26","search_area":"letalske"},
{"count":"385","search_date":"2015-11-26","search_area":"pocitnice"},
{"count":"96","search_date":"2015-11-26","search_area":"potovanja"},
{"count":"382","search_date":"2015-11-27","search_area":"lastni-pre"},
{"count":"56","search_date":"2015-11-27","search_area":"letalske"},
{"count":"244","search_date":"2015-11-27","search_area":"pocitnice"},
{"count":"77","search_date":"2015-11-27","search_area":"potovanja"},
{"count":"205","search_date":"2015-11-28","search_area":"lastni-pre"},
{"count":"40","search_date":"2015-11-28","search_area":"letalske"},
{"count":"303","search_date":"2015-11-28","search_area":"pocitnice"},
{"count":"95","search_date":"2015-11-28","search_area":"potovanja"},
{"count":"442","search_date":"2015-11-29","search_area":"lastni-pre"},
{"count":"59","search_date":"2015-11-29","search_area":"letalske"},
{"count":"515","search_date":"2015-11-29","search_area":"pocitnice"},
{"count":"83","search_date":"2015-11-29","search_area":"potovanja"},
{"count":"565","search_date":"2015-11-30","search_area":"lastni-pre"},
{"count":"125","search_date":"2015-11-30","search_area":"letalske"},
{"count":"550","search_date":"2015-11-30","search_area":"pocitnice"},
{"count":"128","search_date":"2015-11-30","search_area":"potovanja"},
{"count":"519","search_date":"2015-12-01","search_area":"lastni-pre"},
{"count":"78","search_date":"2015-12-01","search_area":"letalske"},
{"count":"489","search_date":"2015-12-01","search_area":"pocitnice"},
{"count":"101","search_date":"2015-12-01","search_area":"potovanja"},
{"count":"230","search_date":"2015-12-02","search_area":"lastni-pre"},
{"count":"56","search_date":"2015-12-02","search_area":"letalske"},
{"count":"244","search_date":"2015-12-02","search_area":"pocitnice"},
{"count":"43","search_date":"2015-12-02","search_area":"potovanja"}]
如何在javascript中创建一个对象,如下所示:
[{
name: 'lastni-pre',
data: [391, 457, ...]},
{
name: 'letalske',
data: [63, 103, ...]},
{
name: 'pocitnice',
data: [501, 509, ...]},
{
name: 'potovanja',
data: [114, 184, ...]
}]
我需要这个对象用于highcharts基本列。
答案 0 :(得分:0)
您可以使用underscore.js库来帮助您。
然后你可以这样做:
_.chain(yourItemArray)
.groupBy('search_area')
.map(function(value, key) {
return {
name: key,
data: _.pluck(value, 'count')
}
})
.value();