【发布时间】:2016-02-05 22:57:02
【问题描述】:
给定一个 for of 循环,分配的变量的值(在此示例中为i)等于array[i] 如果它是一个正常的 for 循环则等于。如何访问i当前所在的数组的索引。
我想要什么
let array = ["one", "two", "three"];
for (let i of array) {
console.log(i);// normally logs cycle one : "one", cycle two : "two", cycle three : "three".
console.log(/*what equals the current index*/);// what I want to log cycle one : 1, cycle two : 2, cycle three : 3.
}
【问题讨论】:
-
我对 javascript 了解不多,但在其他语言中,您会循环遍历数组的长度,然后遍历数组本身。
-
let array = ["one", "two", "three"]; let index=0; for (let i of array) { console.log(i); console.log(index++) }