Js中 for...of 写法如何获取数组元素索引值
一般写法
for (const item of arr) {
// console.log(item)
}此方法我们无法获取到 item 的索引值,因此我们使用解构赋值和 entries() 方法来获取每个元素的值和索引
for(const [index, item] of arr.entries()) {
// console.log(index)
}
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。