当前位置:网站首页>ES6中的for...in/of的使用
ES6中的for...in/of的使用
2022-08-10 20:31:00 【Matcha_ice_cream】
下面以对象和数组为例,介绍一下for…in,for…of以及基本for循环的使用
for…of: 遍历的对象必须是可迭代的对象
可迭代的对象包括: Array,Set,Map,DOM中的NodeList对象,String对象,函数的arguments属性
1、在数组中
ES5中的for循环,正常遍历中 i 指的是数组的下标
ES6中,for…in循环,遍历中的 i 指的是数组的下标
ES6中,for…of循环,遍历中的 i 指的是数组中的值
代码展示:
let arr = [1, 3, 5, 7, 9];
for(let i = 0; i < arr.length; i++) {
console.log('for循环中i是数组的下标:', i);
console.log('arr[i]是数组中的值:', arr[i]);
}
console.log('----------------------------');
for(let i in arr) {
console.log('for...in中i是数组的下标:', i);
}
console.log('----------------------------');
for(let i of arr) {
console.log('for...of中i是数组中的值', i)
}
显示效果:
2、在对象中
不能使用 for…of 遍历对象,因为它不是一个可迭代的对象。
代码展示:
let obj = {
name: "YiBo",
age: 18
}
for(let i in obj) {
console.log('for...in循环中的i值得是对象的键', i);
}
console.log('------------------------------------');
for(let i of obj) {
console.log(i);
}
显示效果:
边栏推荐
- PostgreSQL — 安装及常用命令
- 金鱼哥RHCA回忆录:CL210OpenStack操作的故障排除--章节实验
- mysql----group by、where以及聚合函数需要注意事项
- Auto.js中APP应用相关指令
- 【一致性hash】负载均衡器分发请求
- Transferrin (TF) Modified Paclitaxel (PTX) Liposomes (TF-PTX-LP) | Transferrin (Tf) Modified Curcumin Liposomes
- The 2021 ICPC Asia Shanghai Regional Programming Contest D、E
- A fullGC problem troubleshooting caused by groovy
- .NET现代应用的产品设计 - DDD实践
- (10) Sequence and deserialization of image data
猜你喜欢

2020 ICPC Shanghai Site G

"Distributed Microservice E-commerce" Topic (1) - Project Introduction

《分布式微服务电商》专题(一)-项目简介
[email protected])纳米酶"/>血红素-金纳米颗粒(Heme-AuNP)复合纳米酶|金纳米颗粒核多孔空心碳纳米球壳([email protected])纳米酶

(12) findContours function hierarchy explanation

【图像分类】2018-MobileNetV2

深度学习实战教程(一):感知器

Iridium Ruthenium Alloy/Iridium Oxide Biomimetic Nanozyme | Palladium Nanozyme | GMP-Pd Nanozyme | Gold-Palladium Composite Nanozyme | Ternary Metal Pd-M-Ir Nanozyme |shell nanozyme

@Autowired annotation --required a single bean, but 2 were found causes and solutions

参天生长大模型:昇腾AI如何强壮模型开发与创新之根?
随机推荐
机器学习模型验证:被低估的重要一环
【二叉树】二叉搜索树的后序遍历序列
睡前故事|用Bitmap与AST做一个配置化时长系统
[CNN] Brush SOTA's trick
ACM MM 2022 统一归一化:加速Transformer工业部署的归一化方法
详叙c中的分支与循环
Common functions of Auto.js to find pictures and colors
Rider调试ASP.NET Core时报thread not gc-safe的解决方法
idea插件 协议 。。 公司申请软件用
C 语言 时间函数使用技巧(汇总)
mysql----group by、where以及聚合函数需要注意事项
A fullGC problem troubleshooting caused by groovy
Tf ferritin particles contain cisplatin / oxaliplatin / doxorubicin / methotrexate MTX / paclitaxel PTX and other drugs
多线程与高并发(五)—— 源码解析 ReentrantLock
链表应用----约瑟夫问题
Go程序员进化史
Oracle 的开窗函数使用详解(二)
XML小讲
TortoiseSVN小乌龟的使用
壁仞推出全球最大算力芯片,号称以7nm超越英伟达4nm最新GPU