数组方法foEach()

forEach()方法可以遍历数组

使用方法:

var arr = [1,2,3];

var sum;

// forEach有一个回调函数,提供了三个参数,分别是 value , index , array

forEach(function(value, index, array) {

console.log("每个数组元素" + value); 

console.log("数组的索引" + index);

console.log("数组本身" + array);

数组求和:

console.log( sum += value );

})

 

posted @ 2020-10-10 14:51  前端--张帅  阅读(156)  评论(0)    收藏  举报