jsfor循环是什么意思(JavaScript中三种for循环语句的使用总结for、for...in、for...of)
类别:编程学习 浏览量:2258
时间:2021-10-18 11:32:46 jsfor循环是什么意思
JavaScript中三种for循环语句的使用总结for、for...in、for...of前言
每个接触JS的开发人员都不可避免的与for循环打交道,毕竟这是遍历必不可少的工具之一。JavaScript 中的 for 循环语句相信大家都已经快用厌了,现在有好多文章都在讲怎么减少代码中的 for 循环语句,但是,你又不得不承认它们真的很有用。今天,我来总结一下前端 JavaScript 中三种 for 循环语句。
for
这大概是应用最广的循环语句了吧,简单实用,且大多数时候性能还是在线的,唯一的缺点大概就是太普通,没有特色,导致很多人现在不愿用它。
const array = [4, 7, 9, 2, 6]; for (let index = 0; index < array.length; index++) { const element = array[index]; console.log(element); } // 4, 7, 9, 2, 6
for...in
for...in 语句可以以任意顺序遍历一个对象的除 Symbol 以外的可枚举属性。
const temp = {name: "temp"}; function Apple() { this.color = 'red'; } Apple.prototype = temp; const obj = new Apple(); for (const prop in obj) { console.log(`obj.${ prop } = ${ obj[prop] }`); } // obj.color = red // obj.name = temp
如果你只要考虑对象本身的属性,而不是它的原型,那么使用 getOwnPropertyNames() 或执行 hasOwnProperty() 来确定某属性是否是对象本身的属性。
const temp = {name: "temp"}; function Apple() { this.color = 'red'; } Apple.prototype = temp; const obj = new Apple(); for (const prop in obj) { if (obj.hasOwnProperty(prop)) { console.log(`obj.${ prop } = ${ obj[prop] }`); } } // obj.color = red
当然,也可以用来遍历数组。
const arr = [1, 2, 3, 4, 5]; for (const key in arr) { console.log(key) } // 0,1,2,3,4
使用 for...in 可以遍历数组,但是会存在以下问题:
- index 索引为字符串型数字(注意,非数字),不能直接进行几何运算。
- 遍历顺序有可能不是按照实际数组的内部顺序(可能按照随机顺序)。
所以一般不建议使用 for...in 来遍历数组。
for...of
for...of 语句在可迭代对象(包括 Array,Map,Set,String,TypedArray,arguments 对象等等)上创建一个迭代循环,调用自定义迭代钩子,并为每个不同属性的值执行语句。
const array = ['a', 'b', 'c']; for (const element of array) { console.log(element); } // a // b // c
for...of 和 for...in 的区别:
- for...in 语句以任意顺序迭代对象的可枚举属性。
- for...of 语句遍历可迭代对象定义要迭代的数据。
Object.prototype.objCustom = function () { }; Array.prototype.arrCustom = function () { }; let iterable = [3, 5, 7]; iterable.foo = 'hello'; for (const key in iterable) { console.log(key); // logs 0, 1, 2, "foo", "arrCustom", "objCustom" } // 0, 1, 2, "foo", "arrCustom", "objCustom" for (const key of iterable) { console.log(key); } // 3, 5, 7
使用 for...of 遍历 Map 结构:
let nodes = new Map(); nodes.set("node1", "t1") .set("node2", "t2") .set("node3", "t3"); for (const [node, content] of nodes) { console.log(node, content); } // node1 t1 // node2 t2 // node3 t3
可以看出,使用 for...of 遍历 Map 结构还是挺方便的,推荐使用!
总结
- 如果普通 for 循环用腻了,推荐使用 for...of 来替代。
- 这三种循环都可以使用 break 关键字来终止循环,也可以使用 continue 关键字来跳过本次循环。
- for...of 循环的适用范围最大。
到此这篇关于JavaScript中三种for循环语句使用总结的文章就介绍到这了,更多相关JS中for循环语句内容请搜索开心学习网以前的文章或继续浏览下面的相关文章希望大家以后多多支持开心学习网!
您可能感兴趣
- python pdf文件操作(Python常见读写文件操作实例总结文本、json、csv、pdf等)
- js浮点数运算精度问题(JS超出精度数字问题的解决方法)
- JSON中的特殊字符
- php多维数组怎么转换json(php实现的数组转xml案例分析)
- phpstudy 目录浏览宽度(JspStudy如何设置PHP根目录可编辑)
- js array的所有方法(js 数组 find,some,filter,reduce区别详解)
- MVC中JSON字符长度超出限制
- vue3 响应式的实现过程(Vue3.x使用mitt.js进行组件通信)
- python编写程序读写数据库(详解js文件通过python访问数据库方法)
- tomcat服务如何在eclipse中配置(HBuilderX配置tomcat外部服务器查看编辑jsp界面的方法详解)
- php加密平台(PHP7实现和CryptoJS的AES加密方式互通示例AES-128-ECB加密)
- JS中错误处理
- 使用Console调试js脚本
- js字符串加密解密
- vue引用js库报错(Vue插件报错:Vue.js is detected on this page.问题解决)
- nodejs格式化教程(nodejs利用readline提示输入内容实例代码)
- 一天一冲也算表 麦步,一款待机 21 天的智能手表体验评测(一天一冲也算表)
- 魅族智能手表充电座曝光 Type-C 接口,线座分离设计(魅族智能手表充电座曝光)
- 华为 Watch GT2 Pro 智能手表曝光,新增支持无线充电(华为WatchGT2)
- vivo首款智能手表来了 也有血氧饱和度监测,一次充电18天续航(vivo首款智能手表来了)
- 你知道 七夕 的真正含义吗(你知道七夕的真正含义吗)
- 七夕的寓意(七夕的寓意)
热门推荐
- threejs 三维可视化(three.js模拟实现太阳系行星体系功能)
- docker 清除none镜像(删除docker images中为none的镜像操作)
- 内网nginx配置https详解(Nginx如何配置Http、Https、WS、WSS的方法步骤)
- 使用谷歌浏览器模拟移动端打开网页
- dedecms中的有些功能如何修改(DedeCms参考手册、函数及文件大全)
- 如何提高软件开发团队效率
- django路由原理(详解django中url路由配置及渲染方式)
- python模块都是自己的语法(详解Python_shutil模块)
- 如何让mysql强制设置复杂的密码
- 宝塔nginx配置修改(宝塔面板安装Tengine报错:nginx: [emerg] invalid IPv6 address in resolver)