javascript - 如何比较两个数组并且只返回正确的值
问题描述
我正在尝试比较两个包含时间值的数组。目标是在一个新数组中返回不在当时或两个预订之间的插槽。
const bookings = ["2020-06-12T12:00:00.000Z", "2020-06-12T10:00:00.000Z"];
const slots = ["2020-06-12T11:00:00.000Z", "2020-06-12T12:20:00.000Z", "2020-06-12T13:40:00.000Z", "2020-06-12T15:00:00.000Z"];
这是我比较时间值的功能
const inBetween = (slot, existingBooking) => {
const start = moment(slot);
const end = moment(start).add(80, "m");
existingBooking = moment(existingBooking).utc();
//returns true or false
return existingBooking.isBetween(start, end)
}
我不知道该怎么做。
解决方案
您可以filter
与 结合使用some
。
const availableSlots = slots.filter(s => !bookings.some(eb => inBetween(s, eb))
这些availableSlots
是slots
没有一些的existingBooking
。
推荐阅读
- ios - 为什么我的 iOS 应用程序在后台模式下被操作系统杀死?
- fpga - Quartus:从外部文件添加依赖项
- sql - 我想在laravel中总结数组结果中的一个字段
- android - 使用getPageTitle在kotlin android中找不到字符串资源
- jquery - Jquery/ASP 中的未知未捕获类型错误
- react-native - 响应本机与样式化组件的响应行为
- dataframe - 错误的 x 刻度标签 matplotlib
- material-design-in-xaml - Root DialogHost 给出一个空白窗口
- java - 依赖默认字段初始化 - 编程风格不好?
- r - 从多个页面抓取 html 节点