使用特定规则在事件数组中插入事件

时间:2017-05-03 09:48:19

标签: javascript algorithm momentjs

我列出了某一天的活动。 (事件时间是一个时刻的对象)

[
  { id: 1, start: '2017-05-01 05:00'},
  { id: 2, start: '2017-05-01 08:00'},
  { id: 3, start: '2017-05-01 14:00'},
  { id: 4, start: '2017-05-01 17:00'}
]

我需要在此数组中添加一个事件,以便新事件时间不少于凌晨5点,在上一个事件之后至少3个小时(如果有的话)和下一个事件之前3小时(如果有的话) )。新活动应在同一天进行规划。

在这种情况下,它会在id = 2之后和上午11点插入新事件。

我开始做了一些事情,但结果却是十几个 ifs ,几个小时后我迷失了思绪:

    // for each event, check if there's time before or after (not less than 5am, not more than 11:59pm)
    for(var i = 0; i < events.length; i++) {
        var eventTime = events[i].start.clone();
        var before = eventTime.clone();
        var after = eventTime.clone();
        before.subtract('hours', 3);
        after.add('hours', 3);
        if(i == 0 && !before.hour() < 5 && !before.isBefore(date, 'day')) {
            // first event of the day, new time cannot be before 5am and cannot be on the previous day
            hour = before.hour();
        } else if(i == 0 && !after.isAfter(date, 'day')) {
            // same as above but we test time + 3h
        } else if(i == events.length - 1 && !after.isAfter(date, 'day')) {
            // last event of the day, new time cannot be the next day (after 11:59pm)
            hour = after.hour();
        } else if (i > 0 && i < events.length - 1) {
            // middle events
            // new time should be at least 3H after previous event and 3H before next event
            // ex: 5h, 8h, 12h ok but 5h, 8h, 10h not ok or 5h, 6h, 9h not ok
            var previousEventTime = events[i-1].start.clone();
            var nextEventTime = events[i+1].start.clone();
            if(before.hour() >= previousEventTime.hour() && after.hour() <= nextEventTime.hour()) {

            }
            //
        }
    }

以下是可以使用的代码段:

&#13;
&#13;
var events = [
  { id: 1, start: 'Mon, 01 May 2017 05:00:00 +0200'},
  { id: 2, start: 'Mon, 01 May 2017 08:00:00 +0200'},
  { id: 3, start: 'Mon, 01 May 2017 14:00:00 +0200'},
  { id: 4, start: 'Mon, 01 May 2017 17:00:00 +0200'}
];

 for(var i = 0; i < events.length; i++) {
      var eventTime = moment(events[i].start).clone();
      var before = moment(eventTime).clone();
      var after = moment(eventTime).clone();
      before.subtract(3, 'hours');
      after.add(3, 'hours');
      console.log(eventTime, before, after);
  }
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

花了一天时间,我想出了一个&#34;数学&#34;溶液

在我的数组中,我想在事件 x 和事件 y > ž 即可。 (x是前一个事件,z是下一个事件)

由于我想在每个事件之间至少需要3个小时,我想:

x+3 <= y-3 || y+3 <= z-3

我实施的那个有点不同。我想我需要在2个事件之间至少6个小时才能在中间插入一个新事件。

y-x > =6 || z-y >= 6

由于我的大脑离燃烧不远,所以代码看起来像这样...

        for(var i = 0; i < events.length; i++) {
            var x = null;
            var y = events[i].start.clone().hour();
            var z = null;
            if(i > 0) {
                x = events[i-1].start.clone().hour();
            }
            if(i < events.length -1) {
                z = events[i+1].start.clone().hour();
            }
            // first event or only event
            if(i == 0) {
                // check if removing 3h00 doesn't cross the 5am limit
                if(y - 3 >= 5) {
                    hour = y - 3;
                } else if(z-y >= 6) {
                    // check if there's enough time before next event
                    hour = y + 3;
                }
            }
            // middle event
            if(x && z) {
                if (y-x >= 6) {
                    hour = y - 3;
                } else if (z-y >= 6) {
                    hour = y + 3;
                }
            }
            // last event (can also be first if only 1 element)
            // with !hour we make sure that a "middle" event hour is not overriden if found above
            if(!hour && i == events.length - 1 && i != 0) {
                // check if adding 3h00 doesn't change day (23h00)
                if(y + 3 <= 23) {
                    hour = y + 3;
                } else if(y-x >= 6) {
                    // check if there's enough time after previous event
                    hour = y - 3;
                }
            }
        }
        return hour;

我很高兴接受改进此算法的答案。