首页 > 解决方案 > 用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;
                }
}

标签: javascript

解决方案


使用while循环而不是for如下所述。

  1. 您应该使用%7查找下一个索引,以便在0-6.
  2. 一旦你找到开放日设置foundNextOpeningDay = true,那么循环就可以结束了。
  3. 在块外声明 willOpenNext 变量while,以便整个函数都可以访问它。
  4. 如果未找到开盘日,则增加 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);


推荐阅读