标记预订/保留的时间段 [英] Mark booked/reserved time slots

查看:93
本文介绍了标记预订/保留的时间段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要从总时隙中删除预订的时隙,我该怎么做?

I want to remove booked time slots form the total time slots, How can I do that?

输入:

实际时隙:

[ '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] ,则输出应为:

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'
]

时隙为 [ 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() 您的时隙数组变成对象数组:

You 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%}

这篇关于标记预订/保留的时间段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆