如何过滤与另一个对象列表中的某些条件不匹配的对象列表
How to filter list of objects that doesn't match some condition in another list of objects
我有一些业务的可选工作时间对象数组(5 分钟跳跃)。
此外,我还有一个约会对象数组。
我想过滤可选的工作时间,如果这些时间之间有现有的约会。
我用 moment.js 库尝试了这个解决方案:
const optionalWorkingHours = [
{ from: "05:00", to: "05:05" },
{ from: "05:05", to: "05:10" },
{ from: "05:10", to: "05:15" },
{ from: "05:15", to: "05:20" },
// ....
];
const appointments = [
{ from: "05:00", to: "05:05" },
{ from: "05:30", to: "06:00" },
{ from: "07:50", to: "08:00" }
];
const availableHours = optionalWorkingHours.filter((o) => {
const optionalFrom = moment(o.from, "HH:mm");
const optionalTo = moment(o.to, "HH:mm");
return appointments.some((c) => {
const currentFrom = moment(c.from, "HH:mm");
const currentTo = moment(c.to, "HH:mm");
const isBusyHours =
currentFrom.isSameOrAfter(optionalFrom, "hour") &&
currentTo.isSameOrBefore(optionalTo, "hour");
return !isBusyHours;
});
});
当前结果:重复可用工作时间
预期结果:不与约会数组重叠的可用工作时间数组。
这并不容易,但我认为您的登记 .some()
并不完整。
你能试试这个解决方案吗:
const optionalWorkingHours = [
{ from: "05:00", to: "05:05" }, // no
{ from: "05:05", to: "05:10" },
{ from: "05:10", to: "05:15" },
{ from: "05:15", to: "05:20" },
{ from: "05:20", to: "05:25" },
{ from: "05:25", to: "05:30" },
{ from: "05:30", to: "05:35" }, // no
{ from: "05:35", to: "05:40" }, // no
{ from: "05:40", to: "05:45" }, // no
{ from: "05:45", to: "05:50" }, // no
{ from: "05:50", to: "05:55" }, // no
{ from: "05:55", to: "06:00" }, // no
{ from: "06:00", to: "06:05" }, //
// ....
];
const appointments = [
{ from: "05:00", to: "05:05" },
{ from: "05:30", to: "06:00" },
{ from: "07:50", to: "08:00" }
];
const availableHours = optionalWorkingHours.filter((o) => {
const optionalFrom = moment(o.from, "HH:mm");
const optionalTo = moment(o.to, "HH:mm");
const check = appointments.some((appointment) => {
const appointmentFrom = moment(appointment.from, "HH:mm");
const appointmentTo = moment(appointment.to, "HH:mm");
const isBusyHours =
(optionalFrom.isSame(appointmentFrom) && optionalTo.isSame(appointmentTo)) ||
(optionalFrom.isSame(appointmentFrom) && optionalTo.isBetween(appointmentFrom, appointmentTo)) ||
(optionalFrom.isBetween(appointmentFrom, appointmentTo) && optionalTo.isSame(appointmentTo)) ||
(optionalFrom.isBetween(appointmentFrom, appointmentTo) && optionalTo.isBetween(appointmentFrom, appointmentTo))
return isBusyHours;
});
return !check;
});
console.table(availableHours);
console.log(availableHours);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
我有一些业务的可选工作时间对象数组(5 分钟跳跃)。
此外,我还有一个约会对象数组。
我想过滤可选的工作时间,如果这些时间之间有现有的约会。
我用 moment.js 库尝试了这个解决方案:
const optionalWorkingHours = [
{ from: "05:00", to: "05:05" },
{ from: "05:05", to: "05:10" },
{ from: "05:10", to: "05:15" },
{ from: "05:15", to: "05:20" },
// ....
];
const appointments = [
{ from: "05:00", to: "05:05" },
{ from: "05:30", to: "06:00" },
{ from: "07:50", to: "08:00" }
];
const availableHours = optionalWorkingHours.filter((o) => {
const optionalFrom = moment(o.from, "HH:mm");
const optionalTo = moment(o.to, "HH:mm");
return appointments.some((c) => {
const currentFrom = moment(c.from, "HH:mm");
const currentTo = moment(c.to, "HH:mm");
const isBusyHours =
currentFrom.isSameOrAfter(optionalFrom, "hour") &&
currentTo.isSameOrBefore(optionalTo, "hour");
return !isBusyHours;
});
});
当前结果:重复可用工作时间
预期结果:不与约会数组重叠的可用工作时间数组。
这并不容易,但我认为您的登记 .some()
并不完整。
你能试试这个解决方案吗:
const optionalWorkingHours = [
{ from: "05:00", to: "05:05" }, // no
{ from: "05:05", to: "05:10" },
{ from: "05:10", to: "05:15" },
{ from: "05:15", to: "05:20" },
{ from: "05:20", to: "05:25" },
{ from: "05:25", to: "05:30" },
{ from: "05:30", to: "05:35" }, // no
{ from: "05:35", to: "05:40" }, // no
{ from: "05:40", to: "05:45" }, // no
{ from: "05:45", to: "05:50" }, // no
{ from: "05:50", to: "05:55" }, // no
{ from: "05:55", to: "06:00" }, // no
{ from: "06:00", to: "06:05" }, //
// ....
];
const appointments = [
{ from: "05:00", to: "05:05" },
{ from: "05:30", to: "06:00" },
{ from: "07:50", to: "08:00" }
];
const availableHours = optionalWorkingHours.filter((o) => {
const optionalFrom = moment(o.from, "HH:mm");
const optionalTo = moment(o.to, "HH:mm");
const check = appointments.some((appointment) => {
const appointmentFrom = moment(appointment.from, "HH:mm");
const appointmentTo = moment(appointment.to, "HH:mm");
const isBusyHours =
(optionalFrom.isSame(appointmentFrom) && optionalTo.isSame(appointmentTo)) ||
(optionalFrom.isSame(appointmentFrom) && optionalTo.isBetween(appointmentFrom, appointmentTo)) ||
(optionalFrom.isBetween(appointmentFrom, appointmentTo) && optionalTo.isSame(appointmentTo)) ||
(optionalFrom.isBetween(appointmentFrom, appointmentTo) && optionalTo.isBetween(appointmentFrom, appointmentTo))
return isBusyHours;
});
return !check;
});
console.table(availableHours);
console.log(availableHours);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>