我要从总时隙中删除预订的时隙,我该怎么做?
输入:
实际时隙:
[ '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'
]
如果预订时隙为["11:00-13:00","14:00-15:00"]
,则输出应为:
[{
"slot": "10:00-10:30",
"isBooked": false
},
{
"slot": "10:30-11:00",
"isBooked": false
},
{
"slot": "11:00-11:30",
"isBooked": true
},
{
"slot": "11:30-12:00",
"isBooked": true
},
{
"slot": "12:00-12:30",
"isBooked": true
},
{
"slot": "12:30-13:00",
"isBooked": true
},
{
"slot": "13:00-13:30",
"isBooked": false
},
{
"slot": "13:30-14:00",
"isBooked": false
},
{
"slot": "14:00-14:30",
"isBooked": true
},
{
"slot": "14:30-15:00",
"isBooked": true
},
{
"slot": "15:00-15:30",
"isBooked": false
},
{
"slot": "15:30-16:00",
"isBooked": false
}]
如果预订时隙为["11:15-13:15"]
,则输出应为:
[{
"slot": "10:00-10:30",
"isBooked": false
},
{
"slot": "10:30-11:00",
"isBooked": false
},
{
"slot": "11:00-11:30",
"isBooked": true
},
{
"slot": "11:30-12:00",
"isBooked": true
},
{
"slot": "12:00-12:30",
"isBooked": true
},
{
"slot": "12:30-13:00",
"isBooked": true
},
{
"slot": "13:00-13:30",
"isBooked": false
},
{
"slot": "13:30-14:00",
"isBooked": false
},
{
"slot": "14:00-14:30",
"isBooked": false
},
{
"slot": "14:30-15:00",
"isBooked": false
},
{
"slot": "15:00-15:30",
"isBooked": false
},
{
"slot": "15:30-16:00",
"isBooked": false
}]
我已经尝试过了:
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")
}
}
但不适用于所有情况
我已经更新了输出,上一个工作正常。只是需要有关更新的更多帮助。
答案 0 :(得分:6)
您可以尝试以下方法将时隙数组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));
.as-console-wrapper {min-height: 100%}
答案 1 :(得分:2)
您可以检查该值是在间隔内还是在两端之一。
function remove(from, slots) {
return slots.reduce((r, s) => {
var [sStart, sEnd] = s.split('-');
return r.filter(f => {
var [fStart, fEnd] = f.split('-');
return (fStart < sStart || fEnd > sEnd) && (fStart > sStart || fEnd <= sStart) && (fStart >= sEnd || fEnd < sEnd);
});
}, from);
}
var array = ['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'],
result1 = remove(array, ["11:00-13:00", "14:00-15:00"]),
result2 = remove(array, ["11:15-13:15"]);
console.log(result1);
console.log(result2);
.as-console-wrapper { max-height: 100% !important; top: 0; }