javascript - 用javascript检查数组中的下一个openingHours
问题描述
我有一个代表餐厅的 buinessTimes 的数组,知道当天的当前索引,比如说 5,代表星期六和下一个开放日是星期一,我想显示下一次场地开放的时间。
我正在使用 for 循环来检查这一点,但问题是循环结束,我不知道如何回到数组的开头......
const [openingTimes, setOpeningTimes] = useState([
{day:'lunes', hours: [ { opens: '09:00', closes: '17:30' }, { opens: '21:00', closes: '03:00' } ]},
{day:'martes', hours: [ { opens: '09:00', closes: '17:30' }, { opens: '21:00', closes: '03:00' } ]},
{day:'miércoles', hours: [ { opens: '09:00', closes: '17:30' }, { opens: '21:00', closes: '03:00' } ]},
{day:'jueves', hours: [ { opens: '09:00', closes: '17:30' }, { opens: '21:00', closes: '03:00' } ]},
{day:'viernes', hours: [ { opens: '09:00', closes: '17:30' }, { opens: '21:00', closes: '03:00' } ]},
{day:'sabado', hours: [ { opens: '09:00', closes: '17:30' }, { opens: '21:00', closes: '03:00' } ]},
{day:'domingo', hours: [ ]},
]);
const currentDayIndex = 5;
let foundNextOpeningDay = false;
for(var i=0; i<openingTimes.length; i++)
{
if(i > weekdayIndex && openingTimes[i].hours.length > 0 && !foundNextOpeningDay)
{
let willOpenNext = openingTimes[i].hours[0].opens;
}
}
解决方案
使用while
循环而不是for
如下所述。
- 您应该使用
%7
查找下一个索引,以便在0-6
. - 一旦你找到开放日设置foundNextOpeningDay = true,那么循环就可以结束了。
- 在块外声明 willOpenNext 变量
while
,以便整个函数都可以访问它。 - 如果未找到开盘日,则增加 nextDayIndex 以检查第二天。
在下面试试。
const openingTimes = [
{day:'lunes', hours: [ { opens: '09:00', closes: '17:30' }, { opens: '21:00', closes: '03:00' } ]},
{day:'martes', hours: [ { opens: '09:00', closes: '17:30' }, { opens: '21:00', closes: '03:00' } ]},
{day:'miércoles', hours: [ { opens: '09:00', closes: '17:30' }, { opens: '21:00', closes: '03:00' } ]},
{day:'jueves', hours: [ { opens: '09:00', closes: '17:30' }, { opens: '21:00', closes: '03:00' } ]},
{day:'viernes', hours: [ { opens: '09:00', closes: '17:30' }, { opens: '21:00', closes: '03:00' } ]},
{day:'sabado', hours: [ { opens: '09:00', closes: '17:30' }, { opens: '21:00', closes: '03:00' } ]},
{day:'domingo', hours: [ ]},
];
const currentDayIndex = 5;
// set next day index with % 7 so index will range between 0-6
let nextDayIndex = (currentDayIndex + 1) % 7;
// initialize flag as false
let foundNextOpeningDay = false;
// declare variable outside while block so it will be accessible to entire function
let willOpenNext = "";
while (!foundNextOpeningDay) {
// check for next opening times
if (openingTimes[nextDayIndex].hours.length > 0) {
// if opening times found then set foundNextOpeningDay to true so loop will end
foundNextOpeningDay = true;
// set opening day value to willOpenNext object
willOpenNext = "Day : " + openingTimes[nextDayIndex].day + ", hours : " + openingTimes[nextDayIndex].hours[0].opens;
}
// increment nextDayIndex to check for next day if opening day not found
nextDayIndex = (nextDayIndex + 1) % 7;
}
console.log(willOpenNext);
推荐阅读
- google-cloud-ai-platform-pipelines - 无法在 AI Platform Pipelines 上查看 TFX 可视化
- javascript - 奥赛罗极小极大算法在 React.js 中不起作用
- python - 如何在 Dockerfile CMD 和 ENTRYPOINT 执行模式下正确转义 [] 字符?
- c# - 在后面的代码中将依赖属性绑定到另一个依赖属性
- python - 在 Notebook - Odoo 14 中根据组权限限制添加评论
- reactjs - 使用 Create-React-App 动态导入图像
- spring - 在 Spring Boot 中以编程方式删除和重新创建表
- python - Python textwrap:终端打印与写入文件的不同结果
- php - 比较多个字符串并在php中找到相似的子字符串
- json - 如何使用 jq 写入 JSON 文件而不更改 Powershell 的二进制文件?