gpt4 book ai didi

javascript - 合并所有房间的可用时间段

转载 作者:行者123 更新时间:2023-12-04 06:02:59 27 4
gpt4 key购买 nike

假设我有三个房间,每个房间都有单独的可用时间段和总时间段

总时隙:

["09:00-10:00","10:00-11:00", "11:00-12:00",  "12:00-13:00", "13:00-14:00", "14:00-15:00"]

房间 1 可用于

[ "10:00-11:00", "11:00-12:00",  "12:00-13:00"]

房间 2 可用于

[ "11:00-12:00",  "12:00-13:00", "13:00-14:00"]

Room3 可用于

[ "12:00-13:00", "13:00-14:00", "14:00-15:00"]

我想用房间号过滤掉可用的时间段,所以我希望我的输出是这样的。

[{
slot: "09:00-10:00",
available: false,
space : []
},{
slot: "10:00-11:00",
available: true,
space : ["room1"]
},{
slot: "11:00-12:00",
available: true,
space : ["room1", "room2"]
},{
slot: "12:00-13:00",
available: true,
space : ["room1", "room2", "room3"]
},{
slot: "13:00-14:00",
available: true,
space : ["room2", "room3"]
},{
slot: "14:00-15:00",
available: true,
space : ["room3"]
}]

我尝试过的:

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 room1 = ["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, room1));
.as-console-wrapper {
min-height: 100%
}

最佳答案

我已尽最大努力使它可行。请检查是否满足您的要求。

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

let rooms = {"room100":["11:00-11:30", "13:05-13:35", "14:05-14:15"],"room200":[ "11:00-12:00", "12:00-13:00", "13:00-14:00"],"room300":[ "12:00-13:00", "13:00-14:00", "14:00-15:00"]};
var final = [];
const avail = (ts, room) =>
ts.map(item => {
const[start, end] = item.split('-');
let slot = [];

for(var key in room)
{
let avilable = room[key]
.map(item => item.split('-'))
.some(([bookedStart, bookedEnd]) =>
(start >= bookedStart && start < bookedEnd) ||
(end > bookedStart && end <= bookedEnd) ||
(bookedStart >= start && bookedStart < end));
slot = avilable == false ? [...slot] : [...slot,key];
console.log()
}
const isBooked = slot.length > 0 ? true :false;
final.push({"slot":`${start}-${end}`, "available": isBooked, "space": slot })
//console.log(`${start}-${end}`,isBooked, slot);
})

avail(ts,rooms);

console.log(final)
.as-console-wrapper {min-height: 100%}

关于javascript - 合并所有房间的可用时间段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57213573/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com