循环遍历数组或者对象,for、forEach、for in 、 for of 使用最多

for循环

自Javascript诞生时就有,遍历数组,for 循环的语法如下:

for (语句 1; 语句 2; 语句 3) {

要执行的代码块

}

举例说明

var arr = [1,2,3,4]

for(var i = 0 ; i< arr.length ; i++){

console.log(arr[i]); // 1 2 3 4 (输出结果)

}

forEach循环

从ES5开始 Javascript内置了forEach方法 遍历数组

let arr = ['a', 'b', 'c', 'd']

arr.forEach(function (val, idx, arr) {

console.log(val + ', index = ' + idx) // val是当前元素,index当前元素索引,arr数组

console.log(arr)

})

输出结果

a, index = 0

(4) ["a", "b", "c", "d"]

b, index = 1

(4) ["a", "b", "c", "d"]

c, index = 2

(4) ["a", "b", "c", "d"]

d, index = 3

(4) ["a", "b", "c", "d"]

写法简单了很多,但是也存在一个局限 就是你不能中断循环(使用break语句或使用return语句)。

for in循环

for-in循环实际是为循环”enumerable“对象而设计的

let obj = {a: '1', b: '2', c: '3', d: '4'}

for (let o in obj) {

console.log(o) //遍历的实际上是对象的属性名称 a,b,c,d

console.log(obj[o]) //这个才是属性对应的值1,2,3,4

}

for - in 也可用来循环数组,但一般并不推荐

for of循环

它是ES6中新增加的语法

循环一个数组

let arr = ['China', 'America', 'Korea']

for (let o of arr) {

console.log(o) //China, America, Korea

}

但是它并不能循环一个普通对象

let obj = {a: '1', b: '2', c: '3', d: '4'}

for (let o of obj) {

console.log(o) //Uncaught TypeError: obj[Symbol.iterator] is not a function

}

但是可以循环一个拥有enumerable属性的对象。

如果我们按对象所拥有的属性进行循环,可使用内置的Object.keys()方法

let obj = {a: '1', b: '2', c: '3', d: '4'}

for (let o of Object.keys(obj)) {

console.log(o) // a,b,c,d

}

如果我们按对象所拥有的属性值进行循环,可使用内置的Object.values()方法

let obj = {a: '1', b: '2', c: '3', d: '4'}

for (let o of Object.values(obj)) {

console.log(o) // 1,2,3,4

}

循环一个字符串

let str = 'love'

for (let o of str) {

console.log(o) // l,o,v,e

}

循环一个Map

let iterable = new Map([["a", 1], ["b", 2], ["c", 3]]);

for (let [key, value] of iterable) {

console.log(value);

}

// 1

// 2

// 3

for (let entry of iterable) {

console.log(entry);

}

// [a, 1]

// [b, 2]

// [c, 3]

循环一个Set

let iterable = new Set([1, 1, 2, 2, 3, 3]);

for (let value of iterable) {

console.log(value);

}

// 1

// 2

// 3

循环一个类型化数组

let iterable = new Uint8Array([0x00, 0xff]);

for (let value of iterable) {

console.log(value);

}

// 0

// 255

for、forEach、for in、for of用法的更多相关文章

  1. JS中的 map, filter, some, every, forEach, for in, for of 用法总结和区别

    JS中的 map, filter, some, every, forEach, for in, for of 用法总结和区别  :https://blog.csdn.net/hyupeng1006/a ...

  2. MyBatis从入门到精通(第4章):MyBatis动态SQL【foreach、bind、OGNL用法】

    (第4章):MyBatis动态SQL[foreach.bind.OGNL用法] 4.4 foreach 用法 SQL 语句中有时会使用 IN 关键字,例如 id in (1,2,3).可以使用 ${i ...

  3. c:forEach 标签中varStatus的用法

    c:forEach varStatus属性 current 当前这次迭代的(集合中的)项index  当前这次迭代从 0 开始的迭代索引count  当前这次迭代从 1 开始的迭代计数first 用来 ...

  4. JS中的 map, filter, some, every, forEach, for...in, for...of 用法总结

    1.map 有返回值,返回一个新的数组,每个元素为调用func的结果. let list = [1, 2, 3, 4, 5]; let other = list.map((d, i) => { ...

  5. mybatis 中 foreach collection的三种用法

    foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separator,close. ...

  6. [转载]JS中 map, filter, some, every, forEach, for in, for of 用法总结

    转载地址:http://codebay.cn/post/2110.html 1.map 有返回值,返回一个新的数组,每个元素为调用func的结果. let list = [1, 2, 3, 4, 5] ...

  7. mybatis 中 foreach collection的三种用法(转)

    文章转自 https://blog.csdn.net/qq_24084925/article/details/53790287 oreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集 ...

  8. JS中 map, filter, some, every, forEach, for in, for of 用法总结

    本文转载自:http://blog.csdn.net/gis_swb/article/details/52297343 1.map 有返回值,返回一个新的数组,每个元素为调用func的结果. let ...

  9. JSP中forEach和forTokens循环的用法

    <%@page import="java.util.*"%> <%@ page language="java" contentType=&qu ...

  10. PHP中使用foreach时加&符号的用法

    foreach时加&符号:遍历的同时改变原数组即修改数据或者增加数据. $arr = ['a', 'b', 'c']; foreach ($arr as $key => &$va ...

随机推荐

  1. c++库 c语言接口

    //code in add.cxx #include "add.h" int sample::method() { cout<<"method is call ...

  2. 小白也能轻松上手的Prometheus教程

    这篇文章将承接此前关于使用Prometheus配置自定义告警规则的文章.在本文中,我们将demo安装Prometheus的过程以及配置Alertmanager,使其能够在触发告警时能发送邮件,但我们将 ...

  3. CUBA:如何准备上线

            "在我电脑上是好的呢!"现在看来,这句话更像是调侃开发人员的一个段子,但是"开发环境与生产环境"之间的矛盾依然存在.作为开发者,你需要记住,你写 ...

  4. 【React踩坑记五】React项目中引入并使用react-ace代码编辑插件(自定义列表提示)

    最近有一个引入sql编辑器插件的需求,要求代码高亮显示,代码智能提示,以及支持自定义代码提示列表等功能.中途在自定义代码提示列表中由于没有相关demo,所以踩了一些坑,遂将其整理如下,以便日后查看. ...

  5. 高性能服务器开发基础系列 (二)Reactor模式

    系列目录 第01篇 主线程与工作线程的分工 第02篇 Reactor模式 第03篇 一个服务器程序的架构介绍 第04篇 如何将socket设置为非阻塞模式 第05篇 如何编写高性能日志 第06篇 关于 ...

  6. IIS搭建网站(二)

    win+IIS+ASP+ACCESS第二种搭建方式 安装 控制面板”,依次选“添加/删除程序”, 添加/删除Windows组件 在应用程序服务器前打钩.点击详细信息 将“Internet信息服务(II ...

  7. 慎用ToLower和ToUpper,小心把你的系统给拖垮了

    不知道何时开始,很多程序员喜欢用ToLower,ToUpper去实现忽略大小写模式的字符串相等性比较,有可能这个习惯是从别的语言引进的,大胆猜测下是JS,为了不引起争论,我指的JS是技师的意思~ 一: ...

  8. libevhtp初探

    libevent的evhttp不适合多线程,libevhtp重新设计了libevent的http API,采用了和memcached类似的多线程模型. worker线程的管道读事件的回调函数为htp_ ...

  9. C. Two Arrays(思维DP或组合数学)

    \(首先很容易想到一个O(n^4m)的DP\) \(设dp\ [i]\ [j]\ [q]\ 为长度i,a数组以j结尾,b数组以q结尾(q>=j)\) for(int i=1;i<=n;i+ ...

  10. HC32F003C4PA GPIO Output

    1.打开启动文件,找到并跳转至SystemInit函数 void SystemInit(void) { stc_clk_systickcfg_t stcCfg; // TODO load trim f ...