Reduce () usage

Source: Internet
Author: User

The reduce () method takes a function as an accumulator, and each value (from left to right) in the array begins to shrink, resulting in a value

Parameters

Callback

A function that executes each value in the array, containing four parameters

Previousvalue

The value returned by the last call to the callback, or the provided initial value (InitialValue)

CurrentValue

Elements in an array that are handled in a single sign

Inde

The index of the current element in the book

Array

Array that calls reduce

InitialValue

As the first argument to call Calback for the first time

Demo1:

[0,1,2,3,4].reduce (function (Previousvalue,currenvalue,index,array) {

return previousvalue+currenvalue;

})//result 10

Demo2:

[0,1,2,3,4].reduce (function (Previousvalue,currenvalue,index,array) {

return previousvalue+currenvalue;

},10)//Result 20

Reduce () usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.