获取匹配间隔

问题描述 投票:-3回答:4

所以我从两件简单的事情开始:

  • 带有匹配间隔名称的时间间隔列表。
  • 和currentTime一样(例如"08:58""15:23""02:03")。

let intervals = {
    "08:00 - 09:00" : "Morning yoga",
    "09:00 - 10:00" : "Breakfast",
    "10:00 - 13:00" : "School Period",
    "13:00 - 14:00" : "Basketball",
    "14:00 - 16:00" : "Free Period",
    "16:00 - 17:00" : "Evening Meal",
    "17:00 - 18:00" : "Exercise Period",
    "18:00 - 19:00" : "Shower Block",
    "19:00 - 22:00" : "Evening Free Time",
    "22:00 - 23:00" : "Evening Rollcall",
    "23:00 - 08:00" : "Lights Out"
  }

我想将间隔的名称提取到给定的currentTime,以便我从例如:

  • "08:58"结果:"Morning yoga"
  • "15:23"结果:"Free Period"
  • "02:03"结果:"Lights Out"

到目前为止我得到的是下面的代码,但显然内部有一些错误。

let getSchedule = function(time) {
  let scheduleIndex = 0;
  let current = getMinute(time);
  let intervalArray = []

  for (let key in schedule)
    intervalArray.push([getMinute(key.split(" - ")[0]),
      getMinute(key.split(" - ")[1])
    ]);

  for (let index = 0; index < intervalArray.length; index++) {
    let interval = intervalArray[index]
    if (current >= interval[0] && current < interval[1]) {
      scheduleIndex = index;
      break;
    }
  };
  return schedule[Object.keys(schedule)[scheduleIndex]]
}

let getMinute = function(time) {
  let hour = parseInt(time.split(":")[0]),
    minute = parseInt(time.split(":")[1]);
  return minute + hour * 60;
}

let result = getSchedule("16:22")
console.log(result)

result = getSchedule("02:00")
console.log(result)
<script>
  let schedule = {
    "08:00 - 09:00": "Morning yoga",
    "09:00 - 10:00": "Breakfast",
    "10:00 - 13:00": "School Period",
    "13:00 - 14:00": "Basketball",
    "14:00 - 16:00": "Free Period",
    "16:00 - 17:00": "Evening Meal",
    "17:00 - 18:00": "Exercise Period",
    "18:00 - 19:00": "Shower Block",
    "19:00 - 22:00": "Evening Free Time",
    "22:00 - 23:00": "Evening Rollcall",
    "23:00 - 08:00": "Lights Out"
  }
</script>

编辑:请注意,完整时间将更改状态,以便23:00具有状态Lights Out而22:59具有状态Evening Rollcall

请注意,我无法更改intervals对象。它的结构必须保持。

javascript jquery time intervals
4个回答
0
投票

我以一种不同的方式做到了。对您有所帮助。

let schedule = {
  "08:00 - 09:00": "Morning yoga",
  "09:00 - 10:00": "Breakfast",
  "10:00 - 13:00": "School Period",
  "13:00 - 14:00": "Basketball",
  "14:00 - 16:00": "Free Period",
  "16:00 - 17:00": "Evening Meal",
  "17:00 - 18:00": "Exercise Period",
  "18:00 - 19:00": "Shower Block",
  "19:00 - 22:00": "Evening Free Time",
  "22:00 - 23:00": "Evening Rollcall",
  "23:00 - 08:00": "Lights Out"
}
var getSchedule = function(time) {
let fullDaySchedule = {}
  Object.entries(schedule).forEach(([currentTime, work]) => {
    let startTime = +currentTime.split(" - ")[0].split(":")[0],
      endTime = +currentTime.split(" - ")[1].split(":")[0];

    (startTime > endTime) && (endTime = (23 - startTime) + (23 + endTime))

    for (let index = startTime; index <= endTime; index++) {
      fullDaySchedule[(index % 24)] = work;
    }
  })

  console.log(fullDaySchedule[+time.split(":")[0]])
}

// You can test this
getSchedule("02:00")

1
投票

如果更改数据结构,则会变得更加简单。

const schedule = {
  "Morning yoga": [800, 900],
  "Breakfast": [900, 1000]
};

const getSchedule = timeString => {
  const time = +timeString.replace(":", "");
  const result = [];
  for (const key in schedule) {
    if (time >= schedule[key][0] && time <= schedule[key][1]) {
      result.push(key);
    }
  }
  return result;
};

const result = getSchedule("09:22");
console.log(result);

0
投票

该脚本最初将scheduleIndex设置为0(默认值),并且从不检查是否找到匹配的间隔

您的逻辑将永远不会与“Lights Out”间隔匹配,因为结束值小于起始值,并且您的逻辑不允许这样做。

因此,getScheduleschedule返回“默认”元素,“晨瑜伽”而不是“熄灯”。

可能的解决方案):

  1. 更改逻辑以正确匹配“Lights Out”间隔。
  2. 将“Lights Out”间隔分成两部分,“00:00 - 08:00”和“23:00 - 23:59”并保持当前逻辑
  3. 保持逻辑并添加错误检查(将scheduleIndex设置为无效值并在返回结果之前对其进行测试)并从计划中删除“Lights Out”间隔,并假设任何不匹配的是此默认间隔。 (如下所示)

let getSchedule = function(time) {
  let scheduleIndex = -1;
  let current = getMinute(time);
  let intervalArray = []

  for (let key in schedule)
    intervalArray.push([getMinute(key.split(" - ")[0]),
      getMinute(key.split(" - ")[1])
    ]);

  for (let index = 0; index < intervalArray.length; index++) {
    let interval = intervalArray[index]
    if (current >= interval[0] && current < interval[1]) {
      scheduleIndex = index;
      break;
    }
  };
  return -1 == scheduleIndex? "Lights Out" : schedule[Object.keys(schedule)[scheduleIndex]];
}

let getMinute = function(time) {
  let hour = parseInt(time.split(":")[0]),
    minute = parseInt(time.split(":")[1]);
  return minute + hour * 60;
}

let result = getSchedule("16:22")
console.log(result)

result = getSchedule("02:00")
console.log(result)
<script>
  let schedule = {
    "08:00 - 09:00": "Morning yoga",
    "09:00 - 10:00": "Breakfast",
    "10:00 - 13:00": "School Period",
    "13:00 - 14:00": "Basketball",
    "14:00 - 16:00": "Free Period",
    "16:00 - 17:00": "Evening Meal",
    "17:00 - 18:00": "Exercise Period",
    "18:00 - 19:00": "Shower Block",
    "19:00 - 22:00": "Evening Free Time",
    "22:00 - 23:00": "Evening Rollcall",
  }
</script>

0
投票

我已经重新调整了你的代码a bit。它的功能更强大,我相信可读的方式。此外,我考虑了一些边缘情况(当然不是全部,因为事实并非如此)。看看这个坏男孩:

// Keep in mind that for e.g. `14:00` you have two parameters that fullfil the requirement: 
// `Basketball` and `Free Period`. 
// This constrain should be specified in the code somewhere.
// Otherwise the first value will be taken.
let schedule = {
    "08:00 - 09:00" : "Morning yoga",
    "09:00 - 10:00" : "Breakfast",
    "10:00 - 13:00" : "School Period",
    "13:00 - 14:00" : "Basketball",
    "14:00 - 16:00" : "Free Period",
    "16:00 - 17:00" : "Evening Meal",
    "17:00 - 18:00" : "Exercise Period",
    "18:00 - 19:00" : "Shower Block",
    "19:00 - 22:00" : "Evening Free Time",
    "22:00 - 23:00" : "Evening Rollcall",
    "23:00 - 08:00" : "Lights Out"
  }


let newSchedule = Object
  .entries(schedule)
  .reduce((acc, entry) => {
    const newInterval = {
      from: Number(entry[0].slice(0,5).replace(':','')),
      to: Number(entry[0].slice(-5).replace(':','')),
      value: entry[1],
    };
    return [...acc, newInterval];
  }, [])
  .reduce((acc, { from, to, value}) => {
    // this step is only needed when you can't set ranges in `schedule` object manually
    let intervals = [{ from, to, value }];
    if(from > to) {
      intervals = [
        { 
          from, 
          to: 2400,
          value,
        },
        { 
          from: 0, 
          to,
          value,
        },
      ];
    }
    return [...acc, ...intervals];
  }, []);

const getSchedule = (time) => {
  const timeAsNumber = Number(time.replace(':', ''));
  return newSchedule.find(interval => {
    return timeAsNumber >= interval.from && timeAsNumber <= interval.to;
  }).value
}

console.log(getSchedule('02:00'))
console.log(getSchedule('18:00'))

我鼓励你进一步重构,例如进入更多功能性方法,更高性能,或更干净。

© www.soinside.com 2019 - 2024. All rights reserved.