我想从总时间段中删除预订的时间段,我该怎么做?
输入:
实际时间段:
[ '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'
]
如果 Booked Time Slots 是 ["11:00-13:00","14:00-15:00"]
那么输出应该是:
[ '10:00-10:30',
'10:30-11:00',
'13:00-13:30',
'13:30-14:00',
'15:00-15:30',
'15:30-16:00'
]
如果 Booked Time Slots 是 ["11:15-13:15"]
那么输出应该是:
[ '10:00-10:30',
'10:30-11:00',
'13:30-14:00',
'14:00-14:30',
'14:30-15:00',
'15:00-15:30',
'15:30-16:00'
]
我试过这个:
let actualTimeSlot = []
for(let i = 0; i < times_ara.length; i++) {
if(parseInt(times_ara[i]) < parseInt(timeBooked.split("-")[0])){
actualTimeSlot.push(times_ara[i])
} else if(parseInt(times_ara[i]) > parseInt(timeBooked.split("-")[1])) {
actualTimeSlot.push(times_ara[i])
} else {
console.log("booked")
}
}
但并不是所有情况都适用
最佳答案
您可以尝试使用以下方法将您的时隙数组map()
转换为对象数组:
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'],
booked3 = ["11:00-11:30", "13:05-13:35", "14:05-14:15"],
avail = (ts, booked) =>
ts.map(item => {
const [start, end] = item.split('-'),
isBooked = !booked
.map(item => item.split('-'))
.every(([bookedStart, bookedEnd]) =>
bookedStart >= end || bookedEnd <= start)
return {slot: `${start}-${end}`, isBooked}
})
console.log(avail(ts,booked3))
.as-console-wrapper {min-height: 100%}
关于javascript - 标记已预订/保留的时间段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57073874/