数组的 forEach 方法
有返回值:返回值总是 undefined
Array.forEach(item,index)
// 参数 item 为数组中的每一个元素
// 参数 index 为数组的下标
遍历数组,可看成 for 循环的封装方法
例:求 1-10 和
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let and = 0
arr.forEach(item => {
and += item
})
console.log(and)
// 55
数组的循环遍历方法,数组的forEach方法,数组求和方法
最新推荐文章于 2024-05-17 14:32:46 发布