-2
--------------
|index |value|
|0     |a    |
|1     |b    |
|2     |c    |
|3     |d    |
|4     |e    |
|5     |f    |
|6     |g    |
|7     |h    |
--------------

I need to loop in between index 2 and 7. How could I achieve it?

vrintle
  • 5,501
  • 2
  • 16
  • 46
  • Welcome to Stack Overflow! Can you please provide [a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) of what you've tried so far? Also could be a duplicate with https://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript – Teasel Sep 26 '18 at 07:55
  • You just need to write a simple `for`/`while` loop. – Ram Sep 26 '18 at 07:56

1 Answers1

1

You can use a for ... loop as in the code below:

var myArray = ["a", "b", "c", "d", "e", "f", "g", "h"];

// i = 2 for the starting index
//until you reach index 7
for (var i = 2; i <= 7; i++) {
    console.log(myArray[i]);
    //Do something
}

If you want to replace the 7 by the last case in your array you can do:

var myArray = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"];

// i = 2 for the starting index
//until you reach end of the array
for (var i = 2; i < myArray.length; i++) {
    console.log(myArray[i]);
    //Do something
}
VLAZ
  • 26,331
  • 9
  • 49
  • 67
Teasel
  • 1,330
  • 4
  • 18
  • 25