原始日期结构:
[
{
"DateTimeUtc": "2016-02-02T12:00:00",
"TMP": -0.4,
"DPT": -2.6,
"HUMIDEX": -3.16231537,
},
{
"DateTimeUtc": "2016-02-02T13:00:00",
"TMP": -0.2,
"DPT": -2.8,
"HUMIDEX": -3.00339675,
},
{
"DateTimeUtc": "2016-02-02T14:00:00",
"TMP": -0.3,
"DPT": -2.7,
"HUMIDEX": -13.00339675,
},
.
.
.
]
预期结果:
[
{
"Hour": "DPT",
"Feb 2 2016 5:00AM": -11,
"Feb 2 2016 6:00AM": -11,
"Feb 2 2016 7:00AM": -11,
"Feb 2 2016 8:00AM": -11,
"Feb 2 2016 9:00AM": -10,
"Feb 2 2016 10:00AM": -12,
"Feb 2 2016 11:00AM": -14,
"Feb 2 2016 12:00PM": -13,
"Feb 2 2016 1:00PM": -12,
"Feb 2 2016 2:00PM": -12,
"Feb 2 2016 3:00PM": -12,
"Feb 2 2016 4:00PM": -12,
"Feb 2 2016 5:00PM": -11,
"Feb 2 2016 6:00PM": -11,
"Feb 2 2016 7:00PM": -10,
"Feb 2 2016 8:00PM": -11,
"Feb 2 2016 9:00PM": -10,
"Feb 2 2016 10:00PM": -10,
"Feb 2 2016 11:00PM": -10,
"Feb 3 2016 12:00AM": -10,
"Feb 3 2016 1:00AM": -10,
"Feb 3 2016 2:00AM": -10,
"Feb 3 2016 3:00AM": -10,
"Feb 3 2016 4:00AM": -10
},
{
"Hour": "Humidex",
"Feb 2 2016 5:00AM": -13,
"Feb 2 2016 6:00AM": -13,
"Feb 2 2016 7:00AM": -13,
"Feb 2 2016 8:00AM": -12,
"Feb 2 2016 9:00AM": -13,
"Feb 2 2016 10:00AM": -15,
"Feb 2 2016 11:00AM": -17,
"Feb 2 2016 12:00PM": -15,
"Feb 2 2016 1:00PM": -14,
"Feb 2 2016 2:00PM": -14,
"Feb 2 2016 3:00PM": -13,
"Feb 2 2016 4:00PM": -11,
"Feb 2 2016 5:00PM": -10,
"Feb 2 2016 6:00PM": -10,
"Feb 2 2016 7:00PM": -9,
"Feb 2 2016 8:00PM": -10,
"Feb 2 2016 9:00PM": -9,
"Feb 2 2016 10:00PM": -10,
"Feb 2 2016 11:00PM": -11,
"Feb 3 2016 12:00AM": -11,
"Feb 3 2016 1:00AM": -12,
"Feb 3 2016 2:00AM": -12,
"Feb 3 2016 3:00AM": -13,
"Feb 3 2016 4:00AM": -13
},
.
.
.
]
答案 0 :(得分:1)
我希望这就是你想要的。
function getDate(date) {
var month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oc', 'Nov', 'Dec'][date.getMonth()],
day = date.getDay(),
year = date.getFullYear(),
hours = date.getHours(),
minutes = date.getMinutes(),
ampm = hours >= 12 ? 'PM' : 'AM';
day = day < 10 ? ' ' + day : day;
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
hours = hours < 10 ? ' ' + hours : hours;
minutes = minutes < 10 ? '0' + minutes : minutes;
return month + ' ' + day + ' ' + year + ' ' + hours + ':' + minutes + ampm;
}
var data = [{ "DateTimeUtc": "2016-02-02T12:00:00", "TMP": -0.4, "DPT": -2.6, "HUMIDEX": -3.16231537 }, { "DateTimeUtc": "2016-02-02T13:00:00", "TMP": -0.2, "DPT": -2.8, "HUMIDEX": -3.00339675 }, { "DateTimeUtc": "2016-02-02T14:00:00", "TMP": -0.3, "DPT": -2.7, "HUMIDEX": -13.00339675 }],
cols = ['DPT', 'HUMIDEX'],
pivot = data.reduce(function (r, a) {
cols.forEach(function (k, i) {
r[i] = r[i] || { Hour: k };
r[i][getDate(new Date(a.DateTimeUtc))] = a[k];
});
return r;
}, []);
document.write('<pre>' + JSON.stringify(pivot, 0, 4) + '</pre>');
&#13;