循环

JavaScript 中的 for 循环语句相信大家都已经快用厌了,现在有好多文章都在讲怎么减少代码中的 for 循环语句,但是,你又不得不承认它们真的很有用。今天,我来总结一下前端 JavaScript 中三种 for 循环语句。

for

这大概是应用最广的循环语句了吧,简单实用,且大多数时候性能还是在线的,唯一的缺点大概就是太普通,没有特色,导致很多人现在不愿用它。

  1. const array = [4, 7, 9, 2, 6];
  2. for (const index = 0; index < array.length; index++) {
  3. const element = array[index];
  4. console.log(element);
  5. }
  6. // 4, 7, 9, 2, 6

for…in

for...in 语句可以以任意顺序遍历一个对象的除 Symbol 以外的可枚举属性。

  1. const temp = {name: "temp"};
  2. function Apple() {
  3. this.color = 'red';
  4. }
  5. Apple.prototype = temp;
  6. const obj = new Apple();
  7. for (const prop in obj) {
  8. console.log(`obj.${ prop } = ${ obj[prop] }`);
  9. }
  10. // obj.color = red
  11. // obj.name = temp

如果你只要考虑对象本身的属性,而不是它的原型,那么使用 getOwnPropertyNames() 或执行 hasOwnProperty() 来确定某属性是否是对象本身的属性。

  1. const temp = {name: "temp"};
  2. function Apple() {
  3. this.color = 'red';
  4. }
  5. Apple.prototype = temp;
  6. const obj = new Apple();
  7. for (const prop in obj) {
  8. if (obj.hasOwnProperty(prop)) {
  9. console.log(`obj.${ prop } = ${ obj[prop] }`);
  10. }
  11. }
  12. // obj.color = red

当然,也可以用来遍历数组。

  1. const arr = [1, 2, 3, 4, 5];
  2. for (const key in arr) {
  3. console.log(key)
  4. }
  5. // 0,1,2,3,4

使用 for...in 可以遍历数组,但是会存在以下问题:

  1. index 索引为字符串型数字(注意,非数字),不能直接进行几何运算。

  2. 遍历顺序有可能不是按照实际数组的内部顺序(可能按照随机顺序)。

所以一般不建议使用 for...in 来遍历数组。

for…of

for...of 语句在可迭代对象(包括 Array,Map,Set,String,TypedArray,arguments 对象等等)上创建一个迭代循环,调用自定义迭代钩子,并为每个不同属性的值执行语句。

  1. const array = ['a', 'b', 'c'];
  2. for (const element of array) {
  3. console.log(element);
  4. }
  5. // a
  6. // b
  7. // c

for...offor...in 的区别:

  • for...in 语句以任意顺序迭代对象的可枚举属性

  • for...of 语句遍历可迭代对象定义要迭代的数据

  1. Object.prototype.objCustom = function () { };
  2. Array.prototype.arrCustom = function () { };
  3. let iterable = [3, 5, 7];
  4. iterable.foo = 'hello';
  5. for (const key in iterable) {
  6. console.log(key); // logs 0, 1, 2, "foo", "arrCustom", "objCustom"
  7. }
  8. // 0, 1, 2, "foo", "arrCustom", "objCustom"
  9. for (const key of iterable) {
  10. console.log(key);
  11. }
  12. // 3, 5, 7

使用 for...of 遍历 Map 结构:

  1. let nodes = new Map();
  2. nodes.set("node1", "t1")
  3. .set("node2", "t2")
  4. .set("node3", "t3");
  5. for (const [node, content] of nodes) {
  6. console.log(node, content);
  7. }
  8. // node1 t1
  9. // node2 t2
  10. // node3 t3

可以看出,使用 for...of 遍历 Map 结构还是挺方便的,推荐使用!

总结

  1. 如果普通 for 循环用腻了,推荐使用 for...of 来替代。
  2. 这三种循环都可以使用 break 关键字来终止循环,也可以使用 continue 关键字来跳过本次循环。
  3. for...of 循环的适用范围最大。

~

~ 本文完,感谢阅读!

~

学习有趣的知识,结识有趣的朋友,塑造有趣的灵魂!

我是〖编程三昧〗的作者 隐逸王,我的公众号是『编程三昧』,欢迎关注,希望大家多多指教!

你来,怀揣期望,我有墨香相迎! 你归,无论得失,唯以余韵相赠!

知识与技能并重,内力和外功兼修,理论和实践两手都要抓、两手都要硬!

更多相关文章

  1. PHP:【微信小程序】微信小程序数据交互,微信小程序判断/循环,微信
  2. js 中 for,foreach 遍历数组,与PHP 中 for,foreach 遍历数组的区别-
  3. PHP:PDO->fetch()和fetchAll()遍历,session进行会话跟踪,用户退出
  4. Java 获取 Word 中指定图片的坐标位置
  5. js的for和php的for
  6. (设计模式)迭代器 > 本篇文章由一文多发平台[ArtiPub](https://gi
  7. PHP基础知识:PHP代码书写规范及数组遍历方法
  8. js 中使用for或者forEach可以遍历数组, 请具体举例演绎其与PHP中
  9. php 中的for and foreach遍历数组

随机推荐

  1. 【Android Training - Performance】 -
  2. Android如何保存和读取设置
  3. Android中有用笔记
  4. 在Ubuntu上下载编译安装Android最新内核
  5. Android之UI学习篇十一:ListView控件学习(
  6. Android(安卓)setContentView 实现同一个
  7. getevent工具和Android中inputevent的分
  8. 2011.07.18(4)——— android 播放gif
  9. Android中sdk下载,虚拟机下载,Android环境
  10. 在android中增加公用资源包(类似framework