简体   繁体   English

Javascript:在数组末尾重新启动一次循环

[英]Javascript: Restart loop once at the end of an array

I recently came across this problem and can't find a good answer anywhere (hence the question). 我最近遇到了这个问题,在任何地方都找不到很好的答案(因此,这个问题)。

I want to restart the loop once i reach the end yet only loop a finite amount of times. 我想在结束时重新启动循环,但只循环有限的次数。

In this particular context I have an array of days in the week and i want to display the names of days for the next 7 days from today's day of the week using Date.getDay() ,which, returns a value from 0 (sunday) to 6 (saturday). 在这种特殊情况下,我在一周中有几天的数组,我想使用Date.getDay()显示从今天的星期几开始的未来7天中的日子的名称,它返回一个从0(星期日)开始的值到6(星期六)。 I am able to create an array of the next 7 day names except I keep skipping one because of my current loop. 我可以创建接下来7天名称的数组,但由于当前循环而我一直跳过一个名称。 Here's what i've got so far. 这是我到目前为止所得到的。

My expected output is: 我的预期输出是:

['friday', 'saturday', 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday']

const dayNames = [
  "Monday",
  "Tuesday",
  "Wednesday",
  "Thursday",
  "Friday",
  "Saturday",
  "Sunday"
];
const rawDate = new Date();
let dayNum = rawDate.getDay();
const week = [];
for (let i = 0; i < 6; i++) {
  if (dayNum + 1 === 7) {
    dayNum = 0;
    for (j = 0; j < todayNum; j++) {
      week.push(dayNames[dayNum])
      dayNum++
    }
    week.push(dayNames[dayNum]);
    dayNum++
  } else {
    week.push(dayNames[dayNum + 1]);
    dayNum++;
  }

}
console.log(week);

I do see that my "if" in my "for" is the reason one is skipping but i can't seem to get my head around how to fix. 我确实看到我的“ for”中的“ if”是一个人跳过的原因,但是我似乎无法解决该问题。 Thanks y'all 谢谢你们

You could use the remainder operator % for the right index. 您可以将余数运算符%用于正确的索引。

 const dayNames = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]; const rawDate = new Date(); let dayNum = rawDate.getDay(); const week = []; for (let i = 0; i < 6; i++) { week.push(dayNames[(dayNum + i) % dayNames.length]); } console.log(week); 

You don't need to restart the loop, just use the modulus operator to wrap your array indexes around. 您不需要重新启动循环,只需使用模数运算符即可包装数组索引。

And you need to fix the order of dayNames so it corresponds with what getDay() returns. 并且您需要固定dayNames的顺序,使其与getDay()返回的内容相对应。 And the loop needs to run 7 times, not just 6 to get the entire week. 整个循环需要运行7次,而不仅仅是6次。

 const dayNames = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]; const rawDate = new Date(); const dayNum = rawDate.getDay(); const week = []; for (let i = dayNum; i < dayNum + 7; i++) { week.push(dayNames[i % 7]); } console.log(week); 

Try using the % operator and just one loop: 尝试使用%运算符,仅执行一个循环:

 const dayNames = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]; const rawDate = new Date(); let dayNum = rawDate.getDay(); const week = []; for (let i = 0; i < 7; i++) { week.push(dayNames[dayNum++ % 7]); } console.log(week); 

Also, the zeroeth day of the week, according to Date.prototype.getDate is Sunday. 另外,根据Date.prototype.getDate ,一周的第零天是星期日。

If you want [tomorrow, tomorrow + 1, ..., tomorrow + 6]: 如果要[明天明天+ 1,...,明天+ 6]:

for (let i = 1; i <= 7; i++) {
  week.push(dayNames[(dayNum + i) % 7]);
}

You need i < 7 to get the entire next 7 days, then using the modulus operator % which returns the remainder of division and a single for loop will give you the desired result. 您需要i < 7才能获得接下来7天的全部信息,然后使用模数运算符%返回除法的余数,并且一个for循环将为您提供所需的结果。

 const dayNames = [
  "Monday",
  "Tuesday",
  "Wednesday",
  "Thursday",
  "Friday",
  "Saturday",
  "Sunday"
];
const rawDate = new Date();
let dayNum = rawDate.getDay();
const week = [];
for (let i = 0; i < 7; i++) {
    week.push(dayNames[(dayNum + i) % dayNames.length]);
}
console.log(week);

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

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