首页 > 解决方案 > 如何比较两个数组并且只返回正确的值

问题描述

我正在尝试比较两个包含时间值的数组。目标是在一个新数组中返回不在当时或两个预订之间的插槽。

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

我不知道该怎么做。

标签: javascriptarraysfilter

解决方案


您可以filter与 结合使用some

const availableSlots = slots.filter(s => !bookings.some(eb => inBetween(s, eb))

这些availableSlotsslots没有一些的existingBooking


推荐阅读