简体   繁体   中英

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?

You can use the modulo operator so that your index variable wraps around to 0 once it reaches the length of the ZODIAC array:

 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:

 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:

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.

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.

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. This is one case where it makes sense to use a do -block to keep things simple:

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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