当前位置:网站首页>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);
}
显示效果:
边栏推荐
- 知识图谱Knowledge Graph
- 2021 CybricsCTF
- Water-soluble alloy quantum dot nanozymes|CuMoS nanozymes|porous silicon-based Pt(Au) nanozymes|[email protected] nanomimetic e
- [mysql] 深入分析MySQL版本控制MVCC规则
- Single-click to cancel the function
- 设备管理中数据聚类处理
- 姜还是老的辣,看看老战哥的老底儿和严谨劲儿
- 什么是抽象类?什么时候用?什么是接口?抽象类与接口的区别?
- 优雅退出在Golang中的实现
- 2021DozerCTF
猜你喜欢
Apache DolphinScheduler 3.0.0 正式版发布!
参天生长大模型:昇腾AI如何强壮模型开发与创新之根?
npm‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件
导入FontForge生成字体
Heme - gold nanoparticles (Heme - AuNP) composite nanometer enzyme | gold nanoparticles nuclear porous hollow carbon nanometer spherical shell (Au @ HCNs) nano enzyme
Pt/CeO2 monatomic nanoparticles enzyme | H - rGO - Pt @ Pd NPs enzyme | carbon nanotube load platinum nanoparticles peptide modified nano enzyme | leukemia antagonism FeOPtPEG composite nano enzyme
ACM MM 2022 统一归一化:加速Transformer工业部署的归一化方法
leetcode 85.最大矩形 单调栈应用
Are you hungry - Institution tree radio
【图像分类】2018-MobileNetV2
随机推荐
参天生长大模型:昇腾AI如何强壮模型开发与创新之根?
Date picker component (restrict year to set only displayed months)
Knowledge map Knowledge Graph
什么是抽象类?什么时候用?什么是接口?抽象类与接口的区别?
win10 xbox录屏功能不能录声音怎么办
UE4 - 河流流体插件Fluid Flux
Multifunctional Nanozyme Ag/PANI | Flexible Substrate Nano ZnO Enzyme | Rhodium Sheet Nanozyme | Ag-Rh Alloy Nanoparticle Nanozyme | Iridium Ruthenium Alloy/Iridium Oxide Biomimetic Nanozyme
导入FontForge生成字体
Pt/CeO2 monatomic nanoparticles enzyme | H - rGO - Pt @ Pd NPs enzyme | carbon nanotube load platinum nanoparticles peptide modified nano enzyme | leukemia antagonism FeOPtPEG composite nano enzyme
优雅退出在Golang中的实现
(十二) findContours函数的hierarchy详解
The 2021 ICPC Asia Shanghai Regional Programming Contest D、E
[mysql] 深入分析MySQL版本控制MVCC规则
Single-click to cancel the function
APP application related instructions in Auto.js
cordova installation error Command failed: powershell solution
2021年中国工业互联网安全大赛(福建省选拔赛) 暨首届福建省工业互联网创新大赛
(十二)STM32——NVIC中断优先级管理
《分布式微服务电商》专题(一)-项目简介
svg+元素js实现在图片上描点成框,并获取相对图片的坐标位置