booking range of time

Viewed 24

I have reservations array , I’m making a calendar to see unavailability and availibty In 24 hours day

I need to create  ranges together depending on the ranges around it depending of the time (start and end) maybe merge of hours

 arrayBookings = [
{ 
"bookingId":"ce620b048e4b",
"start":"06:00",
"end":"10:00"
},
{ 
"bookingId":"45089d3d269c",
"start":"08:00",
"end":"11:00"
},
{ 
"bookingId":"4e29cd989b19",
"start":"15:00",
"end":"17:00"
},
{ 
"bookingId":"b5034bf797ca",
"start":"14:00",
"end":"16:00"
},
{ 
"bookingId":"a31f8e9d12bf",
"start":"18:00",
"end":"20:00"
}
]

I need to create a new array with range of unavailability and availablity

The expected result

Object = {
   "unavailability":[
      {
         "id":"1",
         "start":"06:00",
         "end":"11:00"
      },
      {
         "id":"2",
         "start":"14:00",
         "end":"17:00"
      },
      {
         "id":"3",
         "start":"18:00",
         "end":"20:00"
      }
   ],
   "availability":[
      {
         "id":"1",
         "start":"00:00",
         "end":"06:00"
      },
      {
         "id":"2",
         "start":"11:00",
         "end":"14:00"
      },
      {
         "id":"3",
         "start":"17:00",
         "end":"18:00"
      },
      {
         "id":"4",
         "start":"20:00",
         "end":"24:00"
      }
   ]
}
 

Whats I have start :

    var tempArray = [];
var tempArray2 = [];
for (let i = 0; i < arrayBookings.length; i++) {
    tempArray.push({id: arrayBookings[i].id, start: new Date(arrayBookings[i].start).getTime(), end: new Date(arrayBookings[i].end).getTime()});
}
tempArray.sort((a,b) => a.start - b.start);
for(j = 0; j < tempArray.length; j++){
    if(tempArray2.length > 0){
    var index = j - 1;
    console.log(index);
    if(tempArray[j].start > tempArray2[tempArray2.length -1].start && tempArray[j].start > tempArray2[tempArray2.length -1].end){
       tempArray2.push({id: tempArray[j].id, start: tempArray[j].start, end: tempArray[j].end});
    } 
  } else {
    tempArray2.push({id: tempArray[j].id, start: tempArray[j].start, end: tempArray[j].end});
  }
}

Thanks

0 Answers
Related