javascript如何在索引6中开始for循环并在索引3中结束?

时间:2018-09-26 07:53:19

标签: javascript loops

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

我需要在索引2和7之间进行循环。

1 个答案:

答案 0 :(得分:1)

您可以按照以下代码使用for ... loop

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
}

如果要将 7 替换为阵列中的最后一种情况,可以执行以下操作:

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
}