Lets say I have three rooms with individual available time slots and total time slots
total time slots:
["09:00-10:00","10:00-11:00", "11:00-12:00", "12:00-13:00", "13:00-14:00", "14:00-15:00"]
Room1 is available for
[ "10:00-11:00", "11:00-12:00", "12:00-13:00"]
Room2 is available for
[ "11:00-12:00", "12:00-13:00", "13:00-14:00"]
Room3 is available for
[ "12:00-13:00", "13:00-14:00", "14:00-15:00"]
I want to filter out available time slots with room number, so I want my output as this.
[{
slot: "09:00-10:00",
available: false,
space : []
},{
slot: "10:00-11:00",
available: true,
space : ["room1"]
},{
slot: "11:00-12:00",
available: true,
space : ["room1", "room2"]
},{
slot: "12:00-13:00",
available: true,
space : ["room1", "room2", "room3"]
},{
slot: "13:00-14:00",
available: true,
space : ["room2", "room3"]
},{
slot: "14:00-15:00",
available: true,
space : ["room3"]
}]
What I tried:
const ts = ['10:00-10:30','10:30-11:00','11:00-11:30','11:30-12:00','12:00-12:30','12:30-13:00','13:00-13:30','13:30-14:00','14:00-14:30','14:30-15:00','15:00-15:30','15:30-16:00'];
const room1 = ["11:00-11:30", "13:05-13:35", "14:05-14:15"];
const avail = (ts, booked) =>
ts.map(item => {
const[start, end] = item.split('-');
const isBooked = booked
.map(item => item.split('-'))
.some(([bookedStart, bookedEnd]) =>
(start >= bookedStart && start < bookedEnd) ||
(end > bookedStart && end <= bookedEnd) ||
(bookedStart >= start && bookedStart < end));
return {slot: `${start}-${end}`, isBooked};
})
console.log(avail(ts,room1));
.as-console-wrapper {min-height: 100%}
You can do this with
filter
andObject.entries
(given that you have arooms
object). The idea is that for each time slot in thets
array, you want to find (filter
) rooms that have an availability that falls in that time slot.I also recommend abstracting the logic for checking whether a time overlaps a time slot to another function so you can test it separately:
You could take some nested iterating structures and check if a room is available in the wanted slot.
I have try my best to make it workable. Please check this if meets your requirement or not.