javascript - 根据预订的时段将可用时隙数组分成 block

标签 javascript arrays lodash chunks

我有 2 个数组:

  1. 从一天的特定开始时间到结束时间生成时间戳(每个 30 分钟(每个时间戳有 1800 个差异))。
  2. 我从 API 响应中获得了预订时段响应。

我需要合并这两个数组以形成代表可用和预订插槽的对象数组:

let availableTimeslots = [
  1559709000,
  1559710800,
  1559712600,
  1559714400,
  1559716200,
  1559718000,
  1559719800,
  1559721600,
  1559723400,
  1559725200,
  1559727000,
  1559728800,
  1559730600,
  1559732400,
  1559734200,
  1559736000,
  1559737800,
  1559739600
];

let bookedTimeSlots = {
  bookings: [
    {
      timestamp: {
        from: 1559719800,
        to: 1559723400
      }
    },
    {
      timestamp: {
        from: 1559730600,
        to: 1559732400
      }
    }
  ]
};

我需要创建对象数组,例如:

[
  {
    available: true,
    timeslots: [1559709000, 1559710800, 1559712600, 1559714400, 1559716200, 1559718000]
  },
  {
    available: false,
    timeslots: [1559719800, 1559721600, 1559723400]
  },
  {
    available: true,
    timeslots: [1559725200, 1559727000, 1559728800]
  },
  {
    available: false,
    timeslots: [1559730600, 1559732400]
  },
  {
    available: true,
    timeslots: [1559732400, 1559734200, 1559736000, 1559737800, 1559739600]
  }
];

我真的很困惑如何继续这个 我正在考虑将 availableTimeslots 中的值替换为所需的预订时段对象,然后将所有非对象值替换为 {available: true, timeslots: [...]}

bookedTimeSlots.bookings.map((bs, i)=> {
            let ai = availableTimeslots.findIndex(bs.timestamp.from);
            ai > -1 && (availableTimeslots[ai]={available: false, x  : [..._.range(bs.timestamp.from, bs.timestamp.to, 1800)]});
        })

如有任何帮助,我们将不胜感激。

最佳答案

有很多方法可以解决这个问题。简化它的一种方法是利用时隙之间存在已知增量 (1,800) 的事实,因此与其尝试分割时隙数组,不如为给定开始的每个“ block ”生成新的时隙数组和结束时隙。在下面的代码片段中,您可以看到采用这种方法的基本递归解决方案:

const INCREMENT = 1800;

// Utility function to generate an inclusive range
function rangeIncl(start, end, incr = INCREMENT) {
  return start < end ? [start, ...rangeIncl(start + incr, end)] : [end];
}

function timeslotGroups(startTimeslot, endTimeslot, bookings) {
  const [booking, ...restBookings] = bookings;

  if (booking) {
    if (startTimeslot < booking.from) {
      // startTimeslot is before next booking.from; add available group
      return [
        {
          available: true,
          timeslots: rangeIncl(startTimeslot, booking.from - INCREMENT),
        },
        ...timeslotGroups(booking.from, endTimeslot, bookings),
      ];
    }

    if (startTimeslot <= booking.to) {
      // startTimeslot is between booking.from and .to; add not-available group
      return [
        {
          available: false,
          timeslots: rangeIncl(booking.from, booking.to),
        },
        ...timeslotGroups(booking.to + INCREMENT, endTimeslot, restBookings),
      ];
    }

    // startTimeslot is after booking.to; try again with next booking
    return timeslotGroups(startTimeslot, endTimeslot, restBookings);
  }
  
  // No more bookings; add final available group if there are any
  // timeslots left
  return startTimeslot < endTimeslot ? [
    {
      available: true,
      timeslots: rangeIncl(startTimeslot, endTimeslot),
    },
  ] : [];
}

const availableTimeslots = [
  1559709000, 1559710800, 1559712600, 1559714400, 1559716200, 1559718000,
  1559719800, 1559721600, 1559723400, 1559725200, 1559727000, 1559728800,
  1559730600, 1559732400, 1559734200, 1559736000, 1559737800, 1559739600,
];

const bookedTimeslots = {
  bookings: [
    { timestamp: { from: 1559719800, to: 1559723400 }},
    { timestamp: { from: 1559730600, to: 1559732400 }},
  ],
};

const firstTimeslot = availableTimeslots[0];
const lastTimeslot = availableTimeslots[availableTimeslots.length - 1];
// Bookings will be easier to work with as an array of { from, to } objects
const bookings = bookedTimeslots.bookings.map(booking => booking.
timestamp);

const groups = timeslotGroups(firstTimeslot, lastTimeslot, bookings);
console.log(groups);

请注意,此代码假定 bookings 将按时间顺序排列。

关于javascript - 根据预订的时段将可用时隙数组分成 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56462975/

相关文章:

javascript - Angularjs + 使用 $q 的两个 http 请求

Javascript(ES6 + Lodash)将源对象与另一个仅更新公共(public)属性的对象深度合并

javascript - 监听特定房间的 Socket.io 事件

JavaScript 初学者变量混淆

javascript - 减少单个数组求和切片

java - 基元数组的最简单列表包装器

php - 在 PHP 中从线性数组创建关联数组

javascript - 如果对象的元素是数组,如何删除该元素?

javascript - Lo-Dash - 帮助我理解为什么 _.pick 不能按我期望的方式工作

javascript - JQuery 不断改变跨度颜色