js_01_数组遍历

一、数组遍历

1.普通 for 循环

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

 

 优化版:

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

 

 

2.for in 循环

var arr=[1,2,3,4,5,6];
for(var i in arr){
    console.log(arr[i]);
}


//输出 1 2 3 4 5 6

 

3. forEach 循环

Array.forEach(callback) 

var arr=[1,2,3,4,5,6];
arr.forEach(function(v,i){    //v==value 为arr项,i==index 为arr索引
    console.log(i+'  'v );
})
//输出
0  1
1  2
2  3
3  4
4  5
5  6

 

 

二.参考资料

1.JS几种数组遍历方式以及性能分析对比

2.javascript 四种数组遍历方法

 

posted @ 2017-11-09 14:02  shirayner  阅读(165)  评论(0编辑  收藏  举报