I need to split an array of objects in a shape of
{
start_time: "2021-08-23T04:40:59.000Z",
end_time : "2021-08-23T04:41:34.000Z",
data: 'Some data' // not relevant
}
by chunks of 1 hour span, beginning of hour, like: 10.00 - 11.00, 11.00 - 12.00, etc.. on the basis of both start time and end time.
this is what I came up with but its give result with only one time either start or end:
const createRangeKey = (end_time) => {
const hour = new Date(end_time).getHours(); // get the hour
const start = hour - (hour % 1); // normalize to the closest even hour
return `${start}-${start + 1}`; // get the key
};
const result = data.reduce((r, o) => {
const key = createRangeKey(o.end_time); // get the key
if (!r[key]) r[key] = []; // init if not existing on the object
r[key].push(o); // add the object to the key
return r;
}, {});