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 0
Add a ment  | 

2 Answers 2

Reset to default 7

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

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