zhiqingstudy

Be a young person with knowledge and content

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
comment
head sculpture
Code:
Related

The art of interpersonal communication and communication

A code of conduct that regulates and regulates interpersonal relationships

The importance of interpersonal relationships




Unless otherwise specified, all content on this website is original. If the reprinted content infringes on your rights, please contact the administrator to delete it
Contact Email:2380712278@qq.com

Filing number:皖ICP备19012824号