js 数组遍历方式

第一种最常用的:for循环

 for(j = 0; j < arr.length; j++) { }

 优化版for循环

 

for(j = 0,len=arr.length; j < len; j++) {
   
}

 对于数组较大时,优化比较明显;

 第二种:foreach

array.forEach(function(currentValue, index, arr), thisValue)

第三种:for ……in

  for……in 的效率比较低

第四种:map 遍历

 

arr.map(function(n){  
   
});

第五种:for……of 遍历(需要ES6支持)

   

for(let value of arr) {  
   
});

  

posted @ 2018-07-13 10:39  纳尼呢  阅读(107)  评论(0编辑  收藏  举报