Tuesday, 7 May 2019

Group objects based on timestamp

I'm trying to wrap my head around this task in JavaScript. I've a list of objects with timestamps:

{ "timestamp": 2019-04-24 07:04:32.820, "data": 0.54 } 
{ "timestamp": 2019-04-24 08:06:22.820, "data": 0.34 } 
{ "timestamp": 2019-04-24 08:41:42.720, "data": 0.89 } 
{ "timestamp": 2019-04-24 09:11:32.430, "data": 0.14 } 
{ "timestamp": 2019-04-24 09:21:31.213, "data": 0.63 }
{ "timestamp": 2019-04-24 10:06:11.120, "data": 0.85 }  

I want to get the objects grouped hourly based on a given start time. So if the start is 2019-04-24 06:44:00, the first period is from 6:44 - 7:44, then 7:44 - 8:44, 8:44 - 9:44 and 9:44 - 10:44 and so forth. For the input I imagine the following output:

[ { "1": [{ "timestamp": 2019-04-24 07:04:32.820, "data": 0.54 }] }, 
  { "2": [{ "timestamp": 2019-04-24 08:06:22.820, "data": 0.34 }, 
       { "timestamp": 2019-04-24 08:41:42.720, "data": 0.89 }] },
  { "3": [{ "timestamp": 2019-04-24 09:11:32.430, "data": 0.14 }, 
       { "timestamp": 2019-04-24 09:21:31.213, "data": 0.63 }] },
  { "4": [{ "timestamp": 2019-04-24 10:06:11.120, "data": 0.85 }]

I'm pretty lost how to grasp this, any ideas?



from Group objects based on timestamp

No comments:

Post a Comment