Get loop counter/index using for…of syntax in JavaScript

后端 未结 11 1979
走了就别回头了
走了就别回头了 2020-11-28 00:55

Caution:

question still applies to for…of loops.> Don\'t use for…in to iterate over an Array, use it

11条回答
  •  天涯浪人
    2020-11-28 01:22

    How about this

    let numbers = [1,2,3,4,5]
    numbers.forEach((number, index) => console.log(`${index}:${number}`))
    

    Where array.forEach this method has an index parameter which is the index of the current element being processed in the array.

提交回复
热议问题