-1

I have an array like below :

[
    {
      epochTime: 1609669500,
      timeStamp: "2021-01-04 10:25",
      value: 1.1790000371547649e-6
    },
    {
      epochTime: 1609669800,
      timeStamp: "2021-01-04 10:30",
      value: 0.004326555113948416
    },
    {
      epochTime: 1609675200,
      timeStamp: "2021-01-04 12:00",
      value: 0.0003694624756462872
    },
    {
      epochTime: 1609751700,
      timeStamp: "2021-01-04 09:15",
      value: 41479433784.889
    },
    {
      epochTime: 1609755600,
      timeStamp: "2021-01-04 10:20",
      value: 23963947008.0
    },
    {
      epochTime: 1609758600,
      timeStamp: "2021-01-04 11:10",
      value: 27541368832.0
    }
  ]

To display a graph, I want to group the data on an hourly basis : Ex- From the above input, I have data only for the 9th, 10th & 12th hour

something like below :

[
   [1609751700, 2021-01-04 09:15, 41479433784.889],  // for (9-10)th hour
   .
   .
   .

   [1609755600, 2021-01-04 10:20, 23963947008.0],        // for (10-11)th hour
   [1609669500, 2021-01-04 10:25, 1.1790000371547649e-6],
   [1609669800, 2021-01-04 10:30, 0.004326555113948416],
   .
   .
   .

   [1609758600, 2021-01-04 11:10, 27541368832.0]   // for (11-12)th hour
   .
   .
   .
]

I tried something of this sort on each element of the array : Convert a Unix timestamp to time in JavaScript

But gives me the wrong EPOC to DateTime conversion

Pejman Kheyri
  • 4,044
  • 9
  • 32
  • 39

1 Answers1

0

You could get the hours from Epoch with the integer value and group with it.

const
    data = [{ epochTime: 1609669500, timeStamp: "2021-01-04 10:25", value: 1.1790000371547649e-6 }, { epochTime: 1609669800, timeStamp: "2021-01-04 10:30", value: 0.004326555113948416 }, { epochTime: 1609675200, timeStamp: "2021-01-04 12:00", value: 0.0003694624756462872 }, { epochTime: 1609751700, timeStamp: "2021-01-04 09:15", value: 41479433784.889 }, { epochTime: 1609755600, timeStamp: "2021-01-04 10:20", value: 23963947008.0 }, { epochTime: 1609758600, timeStamp: "2021-01-04 11:10", value: 27541368832.0 }],
    result = Object.values(data.reduce((r, { epochTime, timeStamp, value }) => {
        (r[Math.floor(epochTime / 60 / 60)] ??= []).push([epochTime, timeStamp, value]);
        return r
    }, {}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Is there a way to do this with the epoc time, without relying on timeStamp, as timeStamp can change from based on locale. Your solution works, but I am afraid of timeStamp changing based on locale. – Jermy Fields Mar 08 '21 at 14:51
  • @JermyFields—in the OP you say you have "*data only for the 9th, 10th & 12th hour*", which appears to reference the values in the *timeStamp* property, which are local. But then you say you want to group by UTC hour, which will likely provide a different grouping given that some offsets are not full hours (e.g. India Standard Time is +5:30, Australian Central Standard Time is +9:30). – RobG Mar 09 '21 at 09:17