How can the current number of i be accessed in a for of loop?

前端 未结 6 1104
一个人的身影
一个人的身影 2020-12-03 20:54

Given a for of loop the value of the assigned the variable(i for this example) is equal to what array[i] would equal if it was a normal for loop. H

6条回答
  •  失恋的感觉
    2020-12-03 21:52

    You mean this?

    array = ["one", "two", "three"];
    
    for (i = 0; i < array.length; i++) {
      console.log(i); // Logs the current index number;
      console.log(array[i]); // Logs the index matching in the array;
    }
    

    Also a good comment from Kaiido was that you can use this to get the value from the array directly as a variable.

    for (var ind = 0, i=array[ind]; ind < array.length; i=array[++ind]) {
        console.log(i);
        console.log(ind);
    }
    

提交回复
热议问题