Reduce is used as an accumulator to return the results of cumulative processing, often used for summation, etc
Basic syntax: arr. reduce (function() {}, initial value)
No initial value
const arr=[1,5,8] const total=arr.reduce((prev,current)=> prev + current,) console.log(total)//total=14
There are initial values, which will affect the final result
const total1=arr.reduce((prev,current)=> prev+current,10) console.log(total1)//total1=24
Calculate Salary Case
const workers=[{name:'小张',salary:10000},{name:'小李',salary:12000},{name:'小明',salary:13000}] const expenditure=workers.reduce((prev,current)=>{ return prev+current.salary },0) console.log(expenditure)// expenditure=35000