简体   繁体   English

如何基于起始索引在数组中循环/换行?

[英]How to loop through/wrap around an array based on starting index?

var ZODIAC = ["RAT", "OX", "TIGER", "RABBIT", "DRAGON", "SNAKE", "HORSE", "SHEEP", "MONKEY", "ROOSTER", "DOG", "PIG"];
var STARTING_ZODIAC = "MONKEY";

How can I print all the elements in this array starting with Monkey and finishing with sheep? 如何打印此数组中从Monkey开始到绵羊结束的所有元素?

You can use the modulo operator so that your index variable wraps around to 0 once it reaches the length of the ZODIAC array: 您可以使用模运算符,以便您的索引变量在达到ZODIAC数组的长度时会回绕为0

 const ZODIAC = ["RAT", "OX", "TIGER", "RABBIT", "DRAGON", "SNAKE", "HORSE", "SHEEP", "MONKEY", "ROOSTER", "DOG", "PIG"]; const STARTING_ZODIAC = "MONKEY"; const startIndex = ZODIAC.indexOf(STARTING_ZODIAC); console.log(STARTING_ZODIAC); for (let i = startIndex + 1; i !== startIndex; i = (i + 1) % ZODIAC.length) { console.log(ZODIAC[i]); } 

Another method would be to slice the two parts of the array into the proper order first: 另一种方法是,以slice阵列的两个部分首先进入正确的顺序:

 const ZODIAC = ["RAT", "OX", "TIGER", "RABBIT", "DRAGON", "SNAKE", "HORSE", "SHEEP", "MONKEY", "ROOSTER", "DOG", "PIG"]; const STARTING_ZODIAC = "MONKEY"; const startIndex = ZODIAC.indexOf(STARTING_ZODIAC); [ ...ZODIAC.slice(startIndex), ...ZODIAC.slice(0, startIndex) ].forEach(str => console.log(str)); 

6 answers, but none of them use the obvious to me solution: 6个答案,但没有一个答案对我有用:

for (let i = 0; i < ZODIAC.length; i++) {
   console.log(ZODIAC[(startIndex + i) % ZODIAC.length]);
}

Loop 12 times, and use the modulus operator so we can count 4, 5, ... 10, 11, 0, 1, 2, 3. 循环12次,并使用模运算符,以便我们可以计算4、5,... 10、11、0、1、2、3。

Easy enough, just print from the start to the end of the array, and then from beginning of the array to the start. 很容易,只需从数组的开始到结尾进行打印,然后从数组的开始一直打印到开始。

function printZodiacs(startingZodiac, zodiacs) {
  const startIndex = zodiacs.indexOf(startingZodiac);

  // start to end of array
  for (let i = startIndex; i < zodiacs.length; i++) {
    console.log(zodiacs[i]);
  }

  // beginning of array to to start
  for (let i = 0; i < startIndex; i++) {
    console.log(zodiacs[i]);
  }
}

printZodiacs(STARTING_ZODIAC, ZODIAC);

Another fun way to solve it: 解决它的另一种有趣方式:

let doubleZodiac = ZODIAC.concat(ZODIAC);
let start = ZODIAC.indexOf(STARTING_ZODIAC);

for (let i = 0; i < ZODIAC.length; i++) {
  console.log(doubleZodiac[start + i]);
}

This adds a copy of the array to the end, and then just prints 12 from the starting index. 这会将数组的副本添加到末尾,然后仅从起始索引打印12。

What seems most straightforward to me is a do-while loop, starting at the index and then wrapping until you get back to that index. 在我看来,最直接的方法是一个do-while循环,从索引开始,然后进行包装,直到回到该索引为止。 This is one case where it makes sense to use a do -block to keep things simple: 在这种情况下,可以使用do -block使事情变得简单:

 const ZODIAC = ["RAT", "OX", "TIGER", "RABBIT", "DRAGON", "SNAKE", "HORSE", "SHEEP", "MONKEY", "ROOSTER", "DOG", "PIG"]; const STARTING_ZODIAC = "MONKEY"; let i = start_ndx = ZODIAC.indexOf(STARTING_ZODIAC); do { console.log(i, ZODIAC[i++]) i == ZODIAC.length && (i=0) } while (i!==start_ndx) 

Comment: To me, most other answers seem unnecessarily complex (using modulo), or inefficient (copying values and new arrays); 评论:在我看来,大多数其他答案似乎都不必要地复杂(使用模),或者效率低下(复制值和新数组)。 where as seen abvoe, both can be avoided, which in my perspective is easier to maintain 从头到尾都可以避免,在我看来这更容易维护

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

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