
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head><body><script>// 数组reduce方法// arr.reduce(function(){上一次值,当前值},初始值)const arr = [1, 5, 8]// 1.没有初始值// const total = arr.reduce(function (prev, current) {// return prev + current// })// console.log(total)//14// 2.有初始值// const total = arr.reduce(function (prev, current) {// return prev + current// }, 10)// console.log(total)//24// 3.箭头函数写法const total = arr.reduce((prve, current) => prve + current, 10)console.log(total)//24</script>
</body></html>
练习:

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head><body><script>const arr = [{name: '张三',salary: 10000}, {name: '李四',salary: 20000}, {name: '王五',salary: 30000}]// 计算三人薪资的总和const total = arr.reduce((prev, current) => {return prev + current.salary}, 0)console.log(total)//600000// 需求:每人每月涨薪30% ,计算当前总和const total1 = arr.reduce((prev, current) => {return prev + current.salary * 1.3}, 0)console.log(total1)//78000</script>
</body></html>
