【问题标题】:How to split array of object time series to multiple time series based on Time slots?如何根据时隙将对象时间序列数组拆分为多个时间序列?
【发布时间】:2021-10-25 07:07:47
【问题描述】:

我想根据每小时的时间段来拆分我的开始时间和结束时间数据。

input = [
  //Type A, it lies between 7 to 8 time slot
  { "start_time": "2021-08-20T07:00:00.000", "end_time": "2021-08-20T07:25:00.000", "Type": "A" },
  //Type B , Time lies between 8, 9 time slot, so need to split into two new objects
  { "start_time": "2021-08-20T07:25:00.000", "end_time": "2021-08-20T08:39:49.000", "Type": "B" },
  //Type C, Time lies between three slots 8, 9, 10, and 11. SO need to split in 4 objects.
  { "start_time": "2021-08-20T08:39:50.000", "end_time": "2021-08-20T11:02:34.000", "Type": "C" }
]

预期输出:

var output = [
  { "start_time": "2021-08-20T07:00:00.000", "end_time": "2021-08-20T07:25:00.000", "Type": "A" },
  { "start_time": "2021-08-20T07:25:00.000", "end_time": "2021-08-20T08:00:00.000", "Type": "B" },
  { "start_time": "2021-08-20T08:00:00.000", "end_time": "2021-08-20T08:39:49.000", "Type": "B" },
  { "start_time": "2021-08-20T08:39:49.000", "end_time": "2021-08-20T09:00:00.000", "Type": "C" },
  { "start_time": "2021-08-20T09:00:00.000", "end_time": "2021-08-20T10:00:00.000", "Type": "C" },
  { "start_time": "2021-08-20T10:00:00.000", "end_time": "2021-08-20T11:00:00.000", "Type": "C" },
  { "start_time": "2021-08-20T11:00:00.000", "end_time": "2021-08-20T11:02:34.000", "Type": "C" }
] 

我使用moment-range来创建时间槽。

const day_start=moment().startOf('day').hours(7);
const day_end=moment().startOf('day').hours(15)
const day=moment.range(day_start,day_end)
const time_slots = Array.from(day.by('minutes',{step:60}))

我得到的输出:

    const hourly_data = [moment("2021-08-20T07:00:00.000"),
moment("2021-08-20T08:00:00.000"),
moment("2021-08-20T09:00:00.000"),

...
moment("2021-08-20T13:00:00.000"),
moment("2021-08-20T14:00:00.000"),
moment("2021-08-20T1500:00.000")]

我按要求获得了每小时范围,但无法拆分。

任何帮助获得以上输出?

【问题讨论】:

    标签: javascript node.js arrays momentjs


    【解决方案1】:

    您需要将每个输入槽与每个标准时间槽进行比较并计算它们的重叠 - 因此您将自动拆分较大的输入槽。

    var standardTimeSlots = [
      {start_time: "07:00:00", end_time: "08:00:00"},
      {start_time: "08:00:00", end_time: "09:00:00"},
      {start_time: "09:00:00", end_time: "10:00:00"},
      {start_time: "10:00:00", end_time: "11:00:00"},
      {start_time: "11:00:00", end_time: "12:00:00"},
    ];
    
    var inputSlots = [
    //Type A, it lies between 7 to 8 time slot
      { "start_time": "2021-08-20T07:00:00.000", "end_time": "2021-08-20T07:25:00.000", "Type": "A" },
      //Type B , Time lies between 8, 9 time slot, so need to split into two new objects
      { "start_time": "2021-08-20T07:25:00.000", "end_time": "2021-08-20T08:39:49.000", "Type": "B" },
      //Type C, Time lies between three slots 8, 9, 10, and 11. SO need to split in 4 objects.
      { "start_time": "2021-08-20T08:39:50.000", "end_time": "2021-08-20T11:02:34.000", "Type": "C" }
    ];
    
    function computeOverlap(start_1, end_1, start_2, end_2)
    {
      if (start_1 < end_2 && start_2 < end_1)
      {
        return [
          start_1 > start_2 ? start_1 : start_2,
          end_1 < end_2 ? end_1 : end_2
        ];
      }
      else
      {
        return false;
      }
    }
    
    var indexInput, indexStandard, tmpStart, tmpEnd, overlap;
    
    for(indexInput = 0; indexInput < inputSlots.length; indexInput++)
    {
      for(indexStandard = 0; indexStandard < standardTimeSlots.length; indexStandard++)
      {
        overlap = computeOverlap(
          inputSlots[indexInput].start_time.substr(11, 8), 
          inputSlots[indexInput].end_time.substr(11, 8),
          standardTimeSlots[indexStandard].start_time,
          standardTimeSlots[indexStandard].end_time,
        );
        if (overlap) console.log(
          inputSlots[indexInput].start_time.substr(0, 11) + overlap[0] + inputSlots[indexInput].start_time.substr(19), 
          inputSlots[indexInput].end_time.substr(0, 11) + overlap[1] + inputSlots[indexInput].end_time.substr(19))
      }
    }

    【讨论】:

      【解决方案2】:

      您可以通过计算其分钟数除以 60(分钟)来检查每个时间跨度削减多少小时。现在你知道你必须多久分开一次。对于您必须拆分的每个时间跨度,都存在三种情况:

      1. 第一次拆分:以 start_time 开始,以 start_time 加一小时结束
      2. 其他拆分:以最后一项开始,以加一小时结束
      3. 最后拆分:从最后一个项目 end 开始,到 end_time 结束

      也许有更有效的方法,但下面的代码确实有效:

      const input = [
          { "start_time": "2021-08-20T07:00:00.000", "end_time": "2021-08-20T07:25:00.000", "Type": "A" },
          { "start_time": "2021-08-20T07:25:00.000", "end_time": "2021-08-20T08:39:49.000", "Type": "B" },
          { "start_time": "2021-08-20T08:39:50.000", "end_time": "2021-08-20T11:02:34.000", "Type": "C" }
      ]
      
      const output = []
      
      const format = "YYYY-MM-DDTHH:mm:ss.SSS"
      
      input.forEach(span => {
          const end = moment(span.end_time)
          const start0 = moment(span.start_time).minutes(0).seconds(0)
          const duration = moment.duration(end.diff(start0))
          const minutes = duration.asMinutes()
          const countSplits = Math.ceil(minutes / 60)
      
          if(countSplits > 1) {
              for (let index = 0; index < countSplits; index++) {
                  if(index === 0) {
                      output.push({
                          "start_time": span.start_time,
                          "end_time": moment(span.start_time).add(1, 'hour').minutes(0).seconds(0).format(format),
                          "Type": span.Type
                      })
                  }
                  else if(index < countSplits - 1) {
                      output.push({
                          "start_time": moment(span.start_time).add(index, 'hour').minutes(0).seconds(0).format(format),
                          "end_time": moment(span.start_time).add(index + 1, 'hour').minutes(0).seconds(0).format(format),
                          "Type": span.Type
                      })
                  }
                  else {
                      output.push({
                          "start_time": moment(span.start_time).add(index, 'hour').minutes(0).format(format),
                          "end_time": span.end_time,
                          "Type": span.Type
                      })
                  }
              }
          }
          else {
              output.push(span)
          }
      })
      
      console.log(output)
      &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"&gt;&lt;/script&gt;

      【讨论】:

        【解决方案3】:

        如果您希望输出写在您的问题中,您可以尝试这样做。 但是你可以做到这一点,而无需使用range

        在任何情况下,您都必须检查时隙的结束时间是在范围限制之前还是之后,并使用下限

        window['moment-range'].extendMoment(moment); // just declaration
        
        const input = [
          //Type A, it lies between 7 to 8 time slot
          { "start_time": "2021-08-20T07:00:00.000", "end_time": "2021-08-20T07:25:00.000", "Type": "A" },
          //Type B , Time lies between 8, 9 time slot, so need to split into two new objects
          { "start_time": "2021-08-20T07:25:00.000", "end_time": "2021-08-20T08:39:49.000", "Type": "B" },
          //Type C, Time lies between three slots 8, 9, 10, and 11. SO need to split in 4 objects.
          { "start_time": "2021-08-20T08:39:50.000", "end_time": "2021-08-20T11:02:34.000", "Type": "C" }
        ]
        const output = [];
        
        for (const rangeDetails of input) {
          const { start_time, end_time, Type } = rangeDetails;
          const dateTimeStart = moment(start_time);
          const dateTimeEnd = moment(end_time);
        
          const range = moment.range(dateTimeStart, dateTimeEnd);
        
          for (const startDateSlot of range.by('minutes', { step: 60 })) {
            const help = { Type };
            help.start_time = startDateSlot.clone();
            help.end_time = moment(startDateSlot).add(60, 'minutes').isBefore(dateTimeEnd) ? moment(startDateSlot).add(60, 'minutes') : dateTimeEnd.clone();
        
            output.push(help);
          }
        }
        console.log(output);
        <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
        <script src="https://cdn.tutorialjinni.com/moment-range/4.0.2/moment-range.js"></script>

        没有range 的替代方案在那里:

        const input = [
          //Type A, it lies between 7 to 8 time slot
          { 'start_time': '2021-08-20T07:00:00.000', 'end_time': '2021-08-20T07:25:00.000', 'Type': 'A' },
          //Type B , Time lies between 8, 9 time slot, so need to split into two new objects
          { 'start_time': '2021-08-20T07:25:00.000', 'end_time': '2021-08-20T08:39:49.000', 'Type': 'B' },
          //Type C, Time lies between three slots 8, 9, 10, and 11. SO need to split in 4 objects.
          { 'start_time': '2021-08-20T08:39:50.000', 'end_time': '2021-08-20T11:02:34.000', 'Type': 'C' }
        ];
        
        const output = [];
        
        for (const rangeDetails of input) {
          const { start_time, end_time, Type } = rangeDetails;
        
          const dateTimeStart = moment(start_time);
          const dateTimeEnd = moment(end_time);
        
          while (dateTimeStart.isBefore(dateTimeEnd)) {
            const help = { Type, start_time: dateTimeStart.clone() }
            dateTimeStart.add(60, 'minutes'); // This edit dateTimeStart, adding 60 minutes
        
            if (dateTimeStart.isAfter(dateTimeEnd)) help.end_time = dateTimeEnd.clone();
            else help.end_time = dateTimeStart;
        
            output.push(help);
          }
        }
        console.log(output);
        &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"&gt;&lt;/script&gt;

        【讨论】:

        • 你做得很好。
        【解决方案4】:

        使用Array.reduce() 遍历每个对象并使用moment 以分钟为单位计算time difference b/w start_time 和end_time,然后通过计算while 循环内的剩余分钟数来创建时间槽。

        input = [
          { "start_time": "2021-08-20T07:00:00.000", "end_time": "2021-08-20T07:25:00.000", "Type": "A" },
          { "start_time": "2021-08-20T07:25:00.000", "end_time": "2021-08-20T08:39:49.000", "Type": "B" },
          { "start_time": "2021-08-20T08:39:50.000", "end_time": "2021-08-20T11:02:34.000", "Type": "C" }
        ];
        
        const result = input.reduce((arr, {start_time, end_time, Type}) => {
          const dayStart = moment.utc(start_time);
          const dayEnd = moment.utc(end_time);
          let timeDiff = moment.duration(dayEnd.diff(dayStart)).asMinutes();
          if (timeDiff > 60) {
            while(dayStart < dayEnd) {
              const diff = moment.duration(dayEnd.diff(dayStart)).asMinutes();
              const minutes = diff > 60 ? 60 : diff;
              const remainder = minutes - dayStart.minute();
              const newObj = {
                start_time: dayStart.clone(),
                Type
              };
              dayStart.add(remainder, 'minutes');
              newObj.end_time = dayStart.clone();
              arr.push(newObj);
            }
          } else {
             arr.push({
              start_time: dayStart,
              end_time: dayEnd,
              Type
             });
          }
          return arr;
        }, []);
        
        console.log(result);
        &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"&gt;&lt;/script&gt;

        【讨论】:

          猜你喜欢
          • 2019-03-31
          • 2021-10-16
          • 2020-06-18
          • 2020-12-21
          • 2015-03-07
          • 1970-01-01
          • 2013-06-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多