• forEach、for in 、 for of三者的区别


    1、for 原始的遍历:

    其实除了这三种方法以外还有一种最原始的遍历,自Javascript诞生起就一直用的 就是for循环,它用来遍历数组

    var arr = [1,2,3,4]
    for(var i = 0 ; i< arr.length ; i++){
        console.log(arr[i])
    }
    

    2、forEach 从ES5开始 Javascript内置了forEach方法 用来遍历数组

    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语句)

    3、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 也可用来循环数组,但一般并不推荐

    4、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
    }
    

    5、循环一个字符串 

    let str = 'love'
    for (let o of str) {
        console.log(o) // l,o,v,e
    }
    

      

    6、循环一个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]
    

    7、循环一个Set

    let iterable = new Set([1, 1, 2, 2, 3, 3]);
    
    for (let value of iterable) {
      console.log(value);
    }
    // 1
    // 2
    // 3
    

    8、循环一个类型化数组

    let iterable = new Uint8Array([0x00, 0xff]);
    
    for (let value of iterable) {
      console.log(value);
    }
    // 0
    // 255
    

      

     

      

     

     

      

  • 相关阅读:
    错误 1324。文件夹路径 .. 中含有无效的字符
    linux下 tar解压 gz解压 bz2等各种解压文件使用方法
    取消EXCEL 2007/2010中邮箱地址的自动链接
    Windows2000/XP启动过程详解
    Ubuntu下安装apache2,mysql,php,wordpress.
    offcie2007,2010,2012中快速删除指定的页面.
    mysql连接,修改密码,增加用户,显示,导入导出
    键盘各按键的使用
    cvim 使用
    matlab基础知识(basic operation)
  • 原文地址:https://www.cnblogs.com/liubingyjui/p/12620980.html
Copyright © 2020-2023  润新知