【问题标题】:How can I compare two arrays and only return value that are true如何比较两个数组并且只返回正确的值
【发布时间】:2020-06-12 20:25:03
【问题描述】:

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

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

我不知道该怎么做。

【问题讨论】:

    标签: javascript arrays filter


    【解决方案1】:

    您可以将filtersome 结合使用。

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

    availableSlotsslots 没有一些 existingBooking

    【讨论】:

      猜你喜欢
      • 2014-12-08
      • 2015-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多