我想从总时隙中删除预订的时隙,我该怎么做?
输入:
实际时隙:
[ '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'
]
Run Code Online (Sandbox Code Playgroud)
如果预订时间段是,["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'
]
Run Code Online (Sandbox Code Playgroud)
如果预订时间段是,["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'
]
Run Code Online (Sandbox Code Playgroud)
我已经试过了:
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")
}
}
Run Code Online (Sandbox Code Playgroud)
但不适用于所有情况
您可以尝试以下方法将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'];
const booked3 = ["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,booked3));
Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper {min-height: 100%}
Run Code Online (Sandbox Code Playgroud)