简体   繁体   中英

pivot or maybe grouping with js

orginal date structure:

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

Expected result:

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

I hope that is what you want.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM