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

后端 未结 11 1978
走了就别回头了
走了就别回头了 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条回答
  •  猫巷女王i
    2020-11-28 01:21

    On top of the very good answers everyone posted I want to add that the most performant solution is the ES6 entries. It seems contraintuitive for many devs here, so I created this perf benchamrk.

    It's ~6 times faster. Mainly because doesn't need to: a) access the array more than once and, b) cast the index.

提交回复
热议问题