迭代 javascript_如何在JavaScript中的for-of循环中获取迭代的索引

迭代 javascript

A for-of loop, introduced in ES6, is a great way to iterate over an array:

ES6中引入的for-of循环是一种遍历数组的好方法:

for (const v of ['a', 'b', 'c']) {
  console.log(v)
}

How can you get the index of an iteration?

如何获得迭代索引?

The loop does not offer any syntax to do this, but you can combine the destructuring syntax introduced in ES6 with calling the entries() method on the array:

循环不提供任何语法来执行此操作,但是您可以将ES6中引入的解构语法与调用数组上的entrys entries()方法结合使用:

for (const [i, v] of ['a', 'b', 'c'].entries()) {
  console.log(i, v)
}

翻译自: https://flaviocopes.com/how-to-get-index-in-for-of-loop/

迭代 javascript

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值