gpt4 book ai didi

javascript - 枢轴或可能与 js 分组

转载 作者:行者123 更新时间:2023-12-03 07:47:02 26 4
gpt4 key购买 nike

原始数据结构:

    [
{
"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
},
.
.
.
]

最佳答案

我希望这就是你想要的。

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>');

关于javascript - 枢轴或可能与 js 分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35161494/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com