array1.forEach(callbackfn[, thisArg])
参数 |
定义 |
---|---|
array1 |
必选。一个数组对象。 |
callbackfn |
必选。最多可以接受三个参数的函数。对于数组中的每个元素,forEach 都会调用 callbackfn 函数一次。 |
thisArg |
可选。 callbackfn 函数中的 this 关键字可引用的对象。如果省略 thisArg,则 undefined 将用作 this 值。 |
forEach
方法中的function
回调支持3个参数,第1个是遍历的数组内容;第2个是对应的数组索引,第3个是数组本身;
forEach 方法不直接修改原始数组,但回调函数可以修改它
例如
vue中这样改:
this.arr.forEach(function(value,
index, array){
this.arr[index]=value*2;
},this);
普通html页面
arr.forEach(function(value, index, array){
arr[index]=value*2;
});