简体   繁体   English

如何遍历数组并在到达结束后从头开始?

[英]How to loop through an array and continue at beginning once it reaches end?

My problem:我的问题:

I have an array called "weekdays":我有一个名为“工作日”的数组:

const weekdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];

Imagine it is Saturday and I want to know how many days it is until Tuesday (obviously 3 days).假设现在是星期六,我想知道距离星期二还有多少天(显然是 3 天)。 How can I loop through the array - starting at "Sat" and continue at the beginning until it reaches "Tue"?我怎样才能遍历数组 - 从“星期六”开始并从头开始直到它到达“星期二”?

My code so far:到目前为止我的代码:

const weekdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];

const start = weekdays.indexOf("Sat"); 
const end = weekdays.indexOf("Tue"); 
let howManyDays = 0;

for (let i = start; i < end; i = (i + 1) % weekdays.length) {
  howManyDays = howManyDays + 1;
}

However, it seems to be that "howManyDays" is still 0 when I run the code in the console in the browser.但是,当我在浏览器的控制台中运行代码时,“howManyDays”似乎仍然为 0。

UPDATE更新

In order to make this work properly we need to take into account array wrapping - which I didn't in my initial answer.为了使这项工作正常工作,我们需要考虑数组包装——我在最初的回答中没有这样做。

So I'll just leave here a solution similar to the already existing.所以我将在这里留下一个类似于已经存在的解决方案。

 const howManyDaysBetween = (start, end) => { const weekdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; const week = weekdays.length; const startDay = weekdays.indexOf(start); const endDay = weekdays.indexOf(end); const howManyDays = startDay > endDay ? (week - startDay) + endDay : endDay - startDay; console.log(`How many days between ${start} and ${end}?: ${howManyDays}`); } howManyDaysBetween("Sat", "Tue") howManyDaysBetween("Tue", "Sat") howManyDaysBetween("Mon", "Sun") howManyDaysBetween("Fri", "Wed")

const weekdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];

const length = weekdays.length;        // 7
const start = weekdays.indexOf("Sat"); // 5
const end = weekdays.indexOf("Tue");   // 1

let numOfDays = 0;

// If start comes after end, then we can take the difference between the start and the length,
// and add that to the end.
if (start > end) {
  numOfDays = (length - start) + end;
  //               (7 - 5)     + 1     // 3
}

// If start is before end, then just subtract one from the other.
if (start < end) {
  numOfDays = end - start;
}

// If the start and end are the same, then it's 0, which is what our variable was initialised as.
return numOfDays;                      // 0

The only consideration is, whether you want the same day to be 0 , as in the example, or 7 , if it's next week.唯一的考虑是,您是否希望同一天为0 (如示例中所示)或7 (如果下周)。

This loop seems most appropriate to the question asked.这个循环似乎最适合提出的问题。 Although a bit silly if you run 2 indexOf you already got the distance.虽然如果你运行 2 indexOf 有点傻,但你已经获得了距离。 just need to substract and module array length.只需要减去和模块数组长度。 But this approach is good for the loop, because you can just compare the values as you go until you find "Tue"但是这种方法对循环很有用,因为您可以随时比较值,直到找到“Tue”

 const weekdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; const start = weekdays.indexOf("Sat"); const end = weekdays.indexOf("Tue"); let howManyDays = 0; for (let i = start; i != end; i++) { i = i % weekdays.length; howManyDays = howManyDays + 1; } console.log(howManyDays)

You're needing to count in a base-7 numbering system.您需要计算以 7 为基数的编号系统。 The following code will do it simply for you.以下代码将为您简单地完成它。

 const weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']; const start = weekdays.indexOf('Sat'); const end = weekdays.indexOf('Tue') + weekdays.length; const howManyDays = end - start; console.log(howManyDays);

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

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