0

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;
}, {});
madhubala
  • 45
  • 6
  • 4
    You never accepted an answer to your earlier questions. There is [at least one](https://stackoverflow.com/a/66206462/5459839) that deserves to be marked accepted. Your chances of getting good answers will increase if you build reputation (e.g. by accepting answers). – trincot Aug 23 '21 at 15:35

1 Answers1

0

Edited

Note: The keys will be in 24 hour format.

const testVals = {
  start_time: "2021-08-23T04:40:59.000Z",
  end_time : "2021-08-23T06:41:34.000Z",
  data: 'Some data' // not relevant
 }

const createRangeKey = (start_time) => {
    
    const hour = start_time.getHours()
    return `${hour}-${hour + 1}`; // get the key
  };



const getTimeSlots = ( start_time, time_gap) =>{
    const time_list = [start_time]
    const time_slots = []
    for(let i=0 ; i<time_gap ; i++){
    const next_time_slot = new Date(time_list[i])
    next_time_slot.setHours(next_time_slot.getHours() + 1)
    time_list.push(next_time_slot)
    const key = createRangeKey(time_list[i])
    time_slots.push(
      {
        [[key]]: {
          'start_time':time_list[i],
          'end_time':next_time_slot
        }
      }
    )
  }
  return time_slots
}
const result = () =>{
  
  const start_time = new Date(testVals.start_time)
  
  const end_time = new Date(testVals.end_time)
  
  const time_gap = (end_time - start_time) / (1000 * 60 * 60)
  
  const time_slots = getTimeSlots(start_time ,time_gap )
  return time_slots
  
}  

console.log(result())
Ishan Bassi
  • 490
  • 1
  • 5
  • 13
  • Thanks for your efforts. But using your solution, data split according to end time only. I want the split data according to both start_time and end_time. **Example :** if start_time is 11:00 pm and end_time is 7:00 am. so there will be **8 split of one hour span** and every split have 'start_time : 11:00 pm, end_time :12:00 am' and in next split 'start_time : 12:00 am, end_time : 1:00 am' and so on... (with data) – madhubala Aug 24 '21 at 08:55
  • @madhubala , hi , I have edited my answer , so please check it to see if it fits to your needs. It's a little bit messy now. – Ishan Bassi Aug 24 '21 at 17:33