博客
关于我
js数组遍历和对象遍历
阅读量:133 次
发布时间:2019-02-28

本文共 3179 字,大约阅读时间需要 10 分钟。

针对js各种遍历作一个总结分析,从类型用处:分数组遍历和对象遍历;还有性能,优缺点等。

JS数组遍历:

1,普通for循环,经常用的数组遍历

var arr = [1,2,0,3,9]; for ( var i = 0; i 

2,优化版for循环:使用变量,将长度缓存起来,避免重复获取长度,数组很大时优化效果明显

for(var j = 0,len = arr.length; j < len; j++){    console.log(arr[j]);}

3,forEach,ES5推出的,数组自带的循环,主要功能是遍历数组,实际性能比for还弱

arr.forEach(function(value,i){

  console.log('forEach遍历:'+i+'--'+value);

})

forEach这种方法也有一个小缺陷:你不能使用语句中断循环,也不能使用语句返回到外层函数。

4,map遍历,map即是 “映射”的意思 用法与 forEach 相似

arr.map(function(value,index){    console.log('map遍历:'+index+'--'+value);});

map遍历支持使用return语句,支持return返回值

var temp=arr.map(function(val,index){  console.log(val);    return val*val           })console.log(temp);

forEach、map都是ECMA5新增数组的方法,所以ie9以下的浏览器还不支持

5,for-of遍历 是ES6新增功能

for( let i of arr){    console.log(i);}
  • for-of这个方法避开了for-in循环的所有缺陷
  • 与forEach()不同的是,它可以正确响应break、continue和return语句 

for-of循环不仅支持数组,还支持大多数类数组对象,例如DOM 。

for-of循环也支持字符串遍历

JS对象遍历:

1,for-in遍历

for-in是为遍历对象而设计的,不适用于遍历数组。

遍历数组的缺点:数组的下标index值是数字,for-in遍历的index值"0","1","2"等是字符串

for (var index in arr){    console.log(arr[index]);    console.log(index);}

对于对象的遍历,

let obj = {a: 'b',c: 'd'}

一, for in (一般搭配hasOwnProperty来使用) 遍历原型链上可枚举的

let obj = {        a: 'b',        c: 'd'    }    for (let key in obj) {        console.log('key: ' + key + ',' + 'value: ' + obj[key])    }   // key: a,value: b  // key: c,value: d

值得注意的是,for in 遍历对象,会将原型链上的所有可枚举的属性也遍历到。

Object.prototype.test = 'myTest'    let obj = {        a: 'b',        c: 'd'    }    for (let key in obj) {        console.log('key: ' + key + ',' + 'value: ' + obj[key])    }    //key: a,value: b    //key: c,value: d    //key: test,value: myTest

对于不可枚举的属性(enumerable: false),比如说toString方法,那么for in 就不会遍历到

//    Object.prototype.test = 'myTest'    Object.defineProperty(Object.prototype,'test',{        enumerable: false, // 设为不可枚举之后  就无法遍历到,改为true即可遍历到        value: 'myTest'    })    let obj = {        a: 'b',        c: 'd'    }    for (let key in obj) {        console.log('key: ' + key + ',' + 'value: ' + obj[key])    }

由于for in 遍历会遍历原型链上的属性,所以一般我们想要遍历对象本身的属性的时候,要加hasOwnProperty来进行一层过滤。

Object.prototype.test = 'myTest'    let obj = {        a: 'b',        c: 'd'    }    for (let key in obj) {        if (obj.hasOwnProperty(key)) {            console.log('key: ' + key + ',' + 'value: ' + obj[key])        }    }    //key: a,value: b    //key: c,value: d

二,Object.keys() 只遍历自身的可枚举属性

使用Object.keys()只能遍历到自身本身并且是可枚举的属性

Object.prototype.test = 'myTest'    let obj = {        a: 'b',        c: 'd'    }    let keys = Object.keys(obj)    console.log(keys) //["a", "c"]
Object.prototype.test = 'myTest'    let obj = {        a: 'b',        c: 'd'    }    Object.defineProperty(obj,'non-em',{        enumerable: true, //enumerable为false时不可遍历 为true时可以遍历到        value: 'deejay'    })    let keys = Object.keys(obj)    console.log(keys) // ["a", "c", "non-em"]

三, Object.getOwnPropertyNames 遍历所有自身属性 包含不可枚举的

Object.prototype.test = 'myTest'    let obj = {        a: 'b',        c: 'd'    }    Object.defineProperty(obj,'non-em',{        enumerable: false, //enumerable为false时Object.getOwnPropertyNames也能遍历到        value: 'deejay'    })    let keys = Object.getOwnPropertyNames(obj)    console.log(keys)  //["a", "c", "non-em"]

 

转载地址:http://zbod.baihongyu.com/

你可能感兴趣的文章
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>