用ECMAscript5中的forEach函数遍历数组

 

1 var a = [1,2,3];
2 a.forEach(function(value,index,arr){
3     arr[index] = value + index;
4 })
5 console.log(a); >>[1, 3, 5]

// 第一个参数表示值,第二个参数表示索引,第三个参数表示数组本身。2,3参数可以省略。

1 var a = [1,2,3];
2 a.forEach(function(value,index,arr){
3     a[index] = value + index;
4 })
5 console.log(a); >>[1, 3, 5]

// 第三个参数和直接使用a是一样的。

原文地址:https://www.cnblogs.com/zhangxiaolei521/p/5217966.html