简体   繁体   English

使用 javascript 检查数组中的下一个 openingHours

[英]Checking next openingHours in array with javascript

I have an array representing buinessTimes of a restaurant, knowing the current index of the current day, lets say 5, representing saturday and the next opening day being monday, I want to show the next time the venue will open.我有一个代表餐厅的 buinessTimes 的数组,知道当天的当前索引,比如说 5,代表星期六和下一个开放日是星期一,我想显示下一次场地开放的时间。

I'm using a for loop to check this but the problem is the loop ends and I don't know how to go back to the start of the array...我正在使用 for 循环来检查这一点,但问题是循环结束,我不知道如何将 go 回到数组的开头......

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;
                }
}

I think if you want to know the next working hours, use (i+ 1) % 7 to calculate the next day.我想如果您想知道接下来的工作时间,请使用(i+ 1) % 7来计算第二天。

and your question may not be clear enough.你的问题可能还不够清楚。 sunday also has work hours,why is Saturday's next day mondy?星期天也有工作时间,为什么星期六的第二天是星期一?

If you specifically know that sunday will always be closed, then you can just use:如果您特别知道星期日将永远关闭,那么您可以使用:

var nextDay = (currentDayIndex + 1)%6;
var willOpenNext = openingTimes[nextDay].hours[0].opens;

As for why your loop doesn't work, I can see a couple problems:至于为什么你的循环不起作用,我可以看到几个问题:

  1. Using let .使用let In javascript, var is scoped within a function (as in, once you leave a function the variable does not exist), but let and const are scoped within the block .在 javascript 中, var的范围在 function 内(如,一旦你离开 function,变量就不存在了),但letconst的范围在内。 This means that within your loop, because you used let , that you set willOpenNext inside an if, and then immediately leave, destroying that variable.这意味着在您的循环中,因为您使用了let ,所以您将willOpenNext设置在 if 中,然后立即离开,销毁该变量。 You need to use var instead, OR pre-define the variable outside the loop.您需要使用var代替,或者在循环外预定义变量。

  2. You never actually change foundNextOpeningDay .你从来没有真正改变foundNextOpeningDay I can see you are trying to include a check to stop the loop once you have the variable, but you don't actually have a line that sets foundNextOpeningDay = true .我可以看到您正在尝试包含一个检查以在您拥有变量后停止循环,但您实际上并没有设置foundNextOpeningDay = true的行。 May I suggest that an easier way to achieve the same thing, is get rid of the check and just use a break once you find the variable?我是否可以建议一种更简单的方法来实现相同的目标,即摆脱检查并在找到变量后使用break

  3. If the current day is 5, then your first check doesn't work.如果当天是 5,那么您的第一次检查不起作用。

Edit: If Sunday could also possibly have hours, then we can still just use a simple for loop:编辑:如果周日也可能有几个小时,那么我们仍然可以只使用一个简单的 for 循环:

for (let i = (currentDayIndex + 1)%7; i !== currentDayIndex; i = (i + 1)%7) {
  if (openingTimes[i].hours.length) {
    var willOpenNext = openingTimes[i].hours[0].opens;
    break;
  }
}

Use while loop instead of for as explained below.如下所述,使用while循环代替for

  1. You should be using %7 with finding next index so it will produce index between 0-6 .您应该使用%7查找下一个索引,以便生成0-6之间的索引。
  2. Once you found opening day set foundNextOpeningDay = true, so loop can end.一旦你找到开放日设置foundNextOpeningDay = true,那么循环就可以结束了。
  3. Declare willOpenNext variable outside while block so it will be accessible to entire function.while块外声明 willOpenNext 变量,以便整个 function 可以访问它。
  4. Increment nextDayIndex to check for next day if opening day not found.如果未找到开盘日,则增加 nextDayIndex 以检查第二天。

Try it below.在下面试试。

 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM