如何将剩余值设置为最后一个插槽

how to set remaining values to last slots

将所选开始和结束时间的所有总座位数平均分配给创建的空档。但是,如果某些能力 超过了舍入值,那么 distribute/divide 到最后一个插槽的插槽座位容量。 示例:用户已将开始时间选择为明天,开始时间为 11:00,结束时间为 11:45。总座位数为6。 因此,15 分钟时段将创建为:

Timing Seating Capacity
11:00 - 11:15 1
11:15 - 11:30 1
11:30- 11:45 2
11:45 - 12:00 2

我需要上面的结果。我已经使用下面的代码完成了,但没有成功。

    const timePair = timePairs(
      calculate(
        '11:00',
        '12:00'
      )
    );
    console.log(timePair);
    if (timePair.length === values.capacity) {
      console.log(
        map(timePair, (element) => extend({}, element, { capacity: 1 }))
      );
    } else if (values.capacity < timePair.length) {
      console.log(
        map(timePair, (element) =>
          extend({}, element, { capacity: 1 })
        ).slice(0, values.capacity)
      );
    } else {
      if (timePair.length === 1) {
        console.log(
          map(timePair, (element) =>
            extend({}, element, { capacity: values.capacity })
          )
        );
      } else if (timePair.length < values.capacity) {
        console.log(
          map(timePair, (element) =>
            extend({}, element, {
              capacity: Math.round(values.capacity / timePair.length),
            })
          )
        );
      } else {
        const balance = Math.round(values.capacity / timePair.length);
        console.log(balance);
      }
    }

const calculate = (openTime: any, closeTime: any) => {
    const x = {
      slotInterval: 15,
      openTime: openTime,
      closeTime: closeTime,
    };

    const startTime = moment(x.openTime, "HH:mm");
    const endTime = moment(x.closeTime, "HH:mm");

    const allTimes = [];

    while (startTime <= endTime) {
      allTimes.push(startTime.format("HH:mm"));
      startTime.add(x.slotInterval, "minutes");
    }

    return allTimes;
};

const timePairs = (times: any) => {
    const result = [];
    for (let i = 0, n = times.length; i + 1 < n; i += 1) {
      result.push({ start: times[i], end: times[i + 1] });
    }
    return result;
};

以上代码的输出是,

[{"start": "11:00", "end": "11:15", "capacity": 2}, {"start": "11:15", "end": "11:30", "capacity": 2}, {"start": "11:30", "end": "11:45", "capacity": 2}, {"start": "11:45", "end": "12:00", "capacity": 2}]

我希望输出为(如果容量为 6),

[{"start": "11:00", "end": "11:15", "capacity": 1}, {"start": "11:15", "end": "11:30", "capacity": 1}, {"start": "11:30", "end": "11:45", "capacity": 2}, {"start": "11:45", "end": "12:00", "capacity": 2}]

我希望输出为(如果容量为 7),

[{"start": "11:00", "end": "11:15", "capacity": 1}, {"start": "11:15", "end": "11:30", "capacity": 1}, {"start": "11:30", "end": "11:45", "capacity": 2}, {"start": "11:45", "end": "12:00", "capacity": 3}]

终于找到答案了。有相同需求的朋友可以参考下面的代码

console.log(addCapacity(timePair, values.capacity, timePair.length));

const addCapacity = (timePair: any, input: any, num: any) => {
    const extra = input % num;
    const eachTime = Math.floor(input / num);

    const assignment = timePair
      .map((e: any) => {
        if (timePair.indexOf(e) < extra) {
          return eachTime + 1;
        } else {
          return eachTime;
        }
      })
      .reverse();

    assignment.forEach((element: any, index: number) => {
      timePair[index].capacity = element;
    });

    return timePair;
};