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 - 使用 javascript 将文件上传到网站

asp.net - Crystal 报表最佳实践问题

c - 在 C 中传递字符串并填充它们

javascript - Lodash:没有最后一个元素的数组,没有突变

javascript - ng-click 模板中未触发

javascript - 从所有 HTML 元素中移除属性 title

javascript - JavaScript 中是否有一种有效的算法可以在较大的数组集中查找不同数组的数量?

java - 将一组变量中的每个变量设置为数组中的每个项目

javascript - 如何使用lodash深度_sum?

javascript - 映射对象内的数组