es6 for of怎么获取index

2024-11-02 11:10:51

1、打开HBuilder开发工具,新建JavaScript文件for_of.js

es6 for of怎么获取index

2、打开新建的JavaScript文件,使用let定义一个数组a并赋值

es6 for of怎么获取index

3、调用for...of遍历数组a,并打印遍历结果a1和a[a1]

es6 for of怎么获取index

4、保存文件并使用Node Application运行,查看控制台打印结果,第一列为数组元素,第二列为undefined

es6 for of怎么获取index

5、由打印结果看出,index并没有获取;修改for...of语句,将数组转换成Map结果,然后进行遍历

es6 for of怎么获取index

6、再次保存代码并运行文件,可以看到打印结果,第一列为数组索引index,第二列为数组元素

es6 for of怎么获取index
猜你喜欢