微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ES6 for in与for of 的使用方法及其区别

  // for in遍历的是数组的索引(即键名),而for of遍历的是数组元素值。  let arr = [1,2,3,4,5,6,7]     for(let index of arr){      //   console.log(index)//1 2 3 4 5 6 7              }     for(let index in arr){        // console.log(index)//0 1 2 3 4 5 6         //console.log(arr)//1 2 3 4 5 6 7         //console.log(arr[index])     }     //遍历对象 通常用for in来遍历对象的键名     let obj = {         a:1,         b:2,         c:3     }     for(let key in obj){         console.log(key)//a b c          //console.log(obj[key])//1 2 3     }

forEach

  1. 三个参数,第一个value, 第二个 index, 第三个数组体。
  2. 适用于 数组,set,map,不适应于 字符串,Object。
  3. 无法修改删除集合数据,效率和for循环相同,不用关心集合下标的返回。
  4. 不能终止循环,break,continue不能使用。
    栗子:
    let arr = [1, "ding", null, 5, true, undefined];     arr.forEach(function (value, index, a) {         console.log("value:", value, "index:", index, "a:", a);     })  

for in

  1. 索引为字符串
  2. 多用于遍历对象,json,数组,无顺序,增加了转换过程所以开销比较大
  3. 可扩展属性也会遍历
  4. 支持break, continue
    栗子:
 for (let item in obj) {         console.log(item, ":", obj[item]);         if (item == "age") delete obj[item]; //删除属性成功     }     console.log("obj:", obj);  
  1. 是目前遍历数组最好的方法,可以用在set,map,类数组,字符串上,但是不支持原生的Object遍历。
  2. 支持break, continue


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐