博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaScript for..of循环
阅读量:2508 次
发布时间:2019-05-11

本文共 1018 字,大约阅读时间需要 3 分钟。

The for...of loop is my favorite way to loop in JavaScript.

for...of循环是我最喜欢JavaScript循环方式。

It combines the conciseness of forEach loops with the ability to break.

它结合了forEach循环的简洁性和中断能力。

The syntax is this:

语法是这样的:

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

You can break at any point in time using break:

您可以使用break随时break

const list = ['a', 'b', 'c']for (const item of list) {  console.log(item)  if (item === 'b') break}

You can skip an iteration using continue:

您可以使用continue跳过迭代:

const list = ['a', 'b', 'c']for (const item of list) {  if (item === 'b') continue  console.log(item)}

You can get the index of an iteration using entries():

您可以使用entries()获取迭代的索引:

const list = ['a', 'b', 'c']for (const [index, value] of list.entries()) {  console.log(index) //index  console.log(value) //value}

Notice the use of const. The for..of loop creates a new scope in every iteration, so we can safely use that instead of let.

注意const的使用。 for..of循环在每次迭代中都会创建一个新作用域,因此我们可以放心地使用它代替let

翻译自:

转载地址:http://oxmgb.baihongyu.com/

你可能感兴趣的文章
ASP.NET Core 菜鸟之路:从Startup.cs说起
查看>>
vs win32 & MFC 指针默认位置
查看>>
Join 与 CountDownLatch 之间的区别
查看>>
js存cookie
查看>>
vc6下dll调试
查看>>
Ubuntu apt常用命令
查看>>
struts2 配置(部分)
查看>>
python代码迷之错误(ModuleNotFoundError: No module named 'caffe.proto')
查看>>
nodejs adm-zip 解压文件 中文文件名乱码 问题解决
查看>>
阿里程序员:我年薪260万+3000万期权,协和医生一辈子都赚不到
查看>>
MapReduce-文本输入
查看>>
在Linux中简单实现回收子进程
查看>>
<Bootstrap> 学习笔记六. 栅格系统使用案例
查看>>
我的第一个python web开发框架(39)——后台接口权限访问控制处理
查看>>
electron监听系统托盘,electron是否最小化到系统托盘
查看>>
源码学习
查看>>
[DIOCP3-说明书] 关于DEMO的编译
查看>>
Linux系统下部署Tomcat服务器
查看>>
Mac OS X 10.9 下Sublime Text3配置Lua 5.2.3
查看>>
一些性能优化的tips
查看>>