admin管理员组文章数量:1391798
I want to remove booked time slots form the total time slots, How can I do that?
Input:
Actual time slots:
[ '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'
]
if Booked Time Slots is ["11:00-13:00","14:00-15:00"]
then the output should be:
[ '10:00-10:30',
'10:30-11:00',
'13:00-13:30',
'13:30-14:00',
'15:00-15:30',
'15:30-16:00'
]
if Booked Time Slots is ["11:15-13:15"]
then the output should be:
[ '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'
]
I have tried this:
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")
}
}
but it's not working for all the cases
I want to remove booked time slots form the total time slots, How can I do that?
Input:
Actual time slots:
[ '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'
]
if Booked Time Slots is ["11:00-13:00","14:00-15:00"]
then the output should be:
[ '10:00-10:30',
'10:30-11:00',
'13:00-13:30',
'13:30-14:00',
'15:00-15:30',
'15:30-16:00'
]
if Booked Time Slots is ["11:15-13:15"]
then the output should be:
[ '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'
]
I have tried this:
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")
}
}
but it's not working for all the cases
Share Improve this question edited Mar 24, 2020 at 22:56 Yevhen Horbunkov 15.6k3 gold badges27 silver badges45 bronze badges asked Jul 17, 2019 at 10:35 DweepDweep 10413 bronze badges 02 Answers
Reset to default 7You may try the following approach to map()
your timeslots array into array of objects:
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%}
You could check the value if the interval is inside or at one of the ends.
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; }
本文标签: javascriptMark bookedreserved time slotsStack Overflow
版权声明:本文标题:javascript - Mark bookedreserved time slots - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744770137a2624289.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论