遍历数组 优化

var a,
      i = 0,
      arr = [1,2,3,4];
 
while(a = arr[i++]){
    alert(a)                     输出 1,2,3,4 
}
从零开始遍历数组,有点类似于for,但是while 不需要知道数组的长度,只要还有数据就会递增
 
 
 
更好的遍历数组的方法
var i,item;
for(i = 0; item = a[i]; i = i + 1){ }
 
 
for( in ) 的循环速度最慢,因为每次循环都要从实例或者prototype 中寻找属性
如果可以的话用其他循环方式替代,例如
while( i < arr.length ){}
 
while( arr.length = arr.length -1 ){}
 
 
循环数组:
var values = [1, 2, 3, 4, 5, 6];
values.forEach(function(value) {
    console.log(value);
});

Into this:

var values = [1, 2, 3, 4, 5, 6];
for (let value of values) {
    console.log(value);
}
原文地址:https://www.cnblogs.com/chuangweili/p/5160997.html