from:https://www.cnblogs.com/amujoe/p/8875053.html

一、一般的遍历数组的方法:

  1. var array = [1,2,3,4,5,6,7];
  2. for (var i = 0; i < array.length; i) {
  3. console.log(i,array[i]);
  4. }

二、用for in的方遍历数组

  1. for(let index in array) {
  2. console.log(index,array[index]);
  3. };

三、forEach

  1. array.forEach(v=>{
  2. console.log(v);
  3. });
  1. array.forEachfunction(v){
  2. console.log(v);
  3. });
  1.  

四、用for in不仅可以对数组,也可以对enumerable对象操作

  1. var A = {a:1,b:2,c:3,d:"hello world"};
  2. for(let k in A) {
  3. console.log(k,A[k]);
  4. }

五、在ES6中,增加了一个for of循环,使用起来很简单

  1. for(let v of array) {
  2. console.log(v);
  3. };

let s = "helloabc";

for(let c of s) {

console.log(c);

}

  1.  

总结来说:for in总是得到对像的key或数组,字符串的下标,而for of和forEach一样,是直接得到值
结果for of不能对象用
对于新出来的Map,Set上面

  1. var set = new Set();
  2. set.add("a").add("b").add("d").add("c");
  3. var map = new Map();
  4. map.set("a",1).set("b",2).set(999,3);
  5. for (let v of set) {
  6. console.log(v);
  7. }
  8. console.log("--------------------");
  9. for(let [k,v] of map) {
  10. console.log(k,v);
  11. }

javascript遍历对象详细总结

1.原生javascript遍历

(1)for循环遍历

  1. let array1 = ['a','b','c'];
  2. for (let i = 0;i < array1.length;i++){
  3. console.log(array1[i]); // a b c
  4. }

(2)JavaScript 提供了 foreach()  map() 两个可遍历 Array对象 的方    

forEach和map用法类似,都可以遍历到数组的每个元素,而且参数一致;

  1. Array.forEach(function(value , index , array){ //value为遍历的当前元素,index为当前索引,array为正在操作的数组
  2. //do something
  3. },thisArg) //thisArg为执行回调时的this值

不同点:

forEach() 方法对数组的每个元素执行一次提供的函数。总是返回undefined;

  map() 方法创建一个新数组,其结果是该数组中的每个元素都调用一个提供的函数后返回的结果。返回值是一个新的数组;

例子如下:

  1. var array1 = [1,2,3,4,5];
  2.  
  3. var x = array1.forEach(function(value,index){
  4.  
  5. console.log(value); //可遍历到所有数组元素
  6.  
  7. return value + 10
  8. });
  9. console.log(x); //undefined 无论怎样,总返回undefined
  10.  
  11. var y = array1.map(function(value,index){
  12.  
  13. console.log(value); //可遍历到所有数组元素
  14.  
  15. return value + 10
  16. });
  17. console.log(y); //[11, 12, 13, 14, 15] 返回一个新的数组

对于类似数组的结构,可先转换为数组,再进行遍历

  1. let divList = document.querySelectorAll('div'); //divList不是数组,而是nodeList
  2.  
  3. //进行转换后再遍历
  4. [].slice.call(divList).forEach(function(element,index){
  5. element.classList.add('test')
  6. })
  7.  
  8. Array.prototype.slice.call(divList).forEach(function(element,index){
  9. element.classList.remove('test')
  10. })
  11.  
  12. [...divList].forEach(function(element,index){ //<strong>ES6写法</strong>
  13. //do something
  14. })

(3)for ··· in ···     /      for ··· of ···

for...in 语句以任意顺序遍历一个对象的可枚举属性。对于每个不同的属性,语句都会被执行。每次迭代时,分配的是属性名  

补充 : 因为迭代的顺序是依赖于执行环境的,所以数组遍历不一定按次序访问元素。 因此当迭代那些访问次序重要的 arrays 时用整数索引去进行 for 循环 (或者使用 Array.prototype.forEach() 或 for...of 循环) 。

  1. let array2 = ['a','b','c']
  2. let obj1 = {
  3. name : 'lei',
  4. age : '16'
  5. }
  6.  
  7. for(variable in array2){ //variable 为 index
  8. console.log(variable ) //0 1 2
  9. }
  10.  
  11. for(variable in obj1){ //variable 为属性名
  12. console.log(variable) //name age
  13. }

ES6新增了 遍历器(Iterator)机制,为不同的数据结构提供统一的访问机制。只要部署了Iterator的数据结构都可以使用 for ··· of ··· 完成遍历操作  ( Iterator详解 :  http://es6.ruanyifeng.com/#docs/iterator ),每次迭代分配的是 属性值

原生具备 Iterator 接口的数据结构如下:

Array   Map Set String TypedArray 函数的arguments对象 NodeList对象

  1. let array2 = ['a','b','c']
  2. let obj1 = {
  3. name : 'lei',
  4. age : '16'
  5. }
  6.  
  7. for(variable of array2){ //<strong>variable 为 value</strong>
  8. console.log(variable ) //'a','b','c'
  9. }
  10.  
  11. for(variable of obj1){ //<strong>普通对象不能这样用</strong>
  12. console.log(variable) // 报错 : main.js:11Uncaught TypeError: obj1[Symbol.iterator] is not a function
  13. }<br><br>let divList = document.querySelectorAll('div');<br><br>for(element of divlist){ //可遍历所有的div节点<br>  //do something <br>}

如何让普通对象可以用for of 进行遍历呢?  http://es6.ruanyifeng.com/#docs/iterator  一书中有详细说明了!

  除了迭代时分配的一个是属性名、一个是属性值外,for in 和 for of 还有其他不同    (MDN文档: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/for...of)

    for...in循环会遍历一个object所有的可枚举属性。

      for...of会遍历具有iterator接口的数据结构

      for...in 遍历(当前对象及其原型上的)每一个属性名称,而 for...of遍历(当前对象上的)每一个属性值

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

js中forEach,for in,for of循环的用法的更多相关文章

  1. 关于JS中判断是数字和小数的正则表达式用法

    关于JS中判断是数字和小数的正则表达式用法 正则表达式 正则表达式是由一个字符序列形成的搜索模式. 当你在文本中搜索数据时,你可以用搜索模式来描述你要查询的内容. 正则表达式可以是一个简单的字符,或一 ...

  2. js中forEach,for in,for of循环的用法详解

    一.一般的遍历数组的方法: var array = [1,2,3,4,5,6,7]; for (var i = 0; i < array.length; i) { console.log(i,a ...

  3. 十 js中forEach,for in,for of循环的用法

    一.一般的遍历数组的方法: var array = [1,2,3,4,5,6,7]; for (var i = 0; i < array.length; i++) { console.log(i ...

  4. 原生JS中apply()方法的一个值得注意的用法

    今天在学习vue.js的render时,遇到需要重复构造多个同类型对象的问题,在这里发现原生JS中apply()方法的一个特殊的用法: var ary = Array.apply(null, { &q ...

  5. JS中的call()、apply() 以及 bind()方法用法总结

    JS中的call()方法和apply()方法用法总结  : 讲解: 调用函数,等于设置函数体内this对象的值,以扩充函数赖以运行的作用域. function add(c,d){ return thi ...

  6. JS中forEach和map的区别

    共同点: 1.都是循环遍历数组中的每一项. 2.forEach()和map()里面每一次执行匿名函数都支持3个参数:数组中的当前项item,当前项的索引index,原始数组input. 3.匿名函数中 ...

  7. js中 forEach 和 map 区别

    共同点: 1.都是循环遍历数组中的每一项. 2.forEach()和map()里面每一次执行匿名函数都支持3个参数:数组中的当前项item,当前项的索引index,原始数组input. 3.匿名函数中 ...

  8. js中forEach无法跳出循环?

    1. forEach() forEach() 方法从头至尾遍历数组,为每个元素调用指定的函数.如上所述,传递的函数作为forEach()的第一个参数.然后forEach()使用三个参数调用该 函数:数 ...

  9. js 中 forEach 和 map

    共同点: 1.都是循环遍历数组中的每一项. 2.forEach() 和 map() 里面每一次执行匿名函数都支持3个参数:数组中的当前项item,当前项的索引index,原始数组input. 3.匿名 ...

随机推荐

  1. vuex基本使用

    1.组件之间共享数据的方式 父向子传值:v-bind 属性绑定 子向父传值:v-on 事件绑定 兄弟组件之间共享数据:EventBus $on 接收数据的那个组件 $emit 发送数据的那个组件 2. ...

  2. 洛谷P2786 英语1(eng1)- 英语作文——map

    给一手链接 https://www.luogu.com.cn/problem/P2786 拿这道题当map模板练练手qwq #include<cstdio> #include<cst ...

  3. 用Emacs编写mybatis

    用Emacs编写mybatis */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #839496;} ...

  4. 如何为自己的网站添加HTTPS服务

    如何为自己的网站添加HTTPS服务,针对单个域名而言的,下面介绍网站添加https方法,拿阿里云方法 1.准备证书文件 进入阿里云管理控制台-安全-证书服务点击购买证书服务,进入证书购买页面(放心,我 ...

  5. Nginx学习总结:geo与image模块(四

    斜体下划线,表示建议采用默认配置,无需显式的配置 一.ngx_http_geo_module 核心特性为:根据客户端IP(段),geo模块将会匹配出指定的变量(比如,国家代码,城市代码).geo模块可 ...

  6. 微信小程序(4)--二维码窗口

    微信小程序二维码窗口: <view class="btn" bindtap="powerDrawer" data-statu="open&quo ...

  7. 脚本_检测 MySQL 数据库连接数量

    #!bin/bash#功能:检测 MySQL数据库连接数量,以满足对 MySQL 数据库的监控需求,查看 MySQL 连接是否正常.#作者:liusingbon#本脚本每 2 秒检测一次 MySQL ...

  8. Java疯狂讲义笔记——内部类

    [定义]内部类:定义在其它类内部的类.外部类:包含内部类的类,也称 宿主类.局部内部类:定义在方法里的内部类. [接口内部类]接口中也可以定义内部类,必须为public static修饰(自动添加), ...

  9. ThreadPoolTaskExecutor使用详解

    当我们需要实现并发.异步等操作时,通常都会使用到ThreadPoolTaskExecutor,现对其使用稍作总结. 配置ThreadPoolTaskExecutor通常通过XML方式配置,或者通过Ex ...

  10. MFC程序执行过程剖析(转)

    一 MFC程序执行过程剖析 1)我们知道在WIN32API程序当中,程序的入口为WinMain函数,在这个函数当中我们完成注册窗口类,创建窗口,进入消息循环,最后由操作系统根据发送到程序窗口的消息调用 ...