S1: array Iteration Method

Source: Internet
Author: User

Ecmascript 5 also adds two methods to merge Arrays: reduce () and reduceright (). Both methods overlap.
All the items in the algebra group, and then construct a final returned value. The reduce () method starts from the first entry of the array and traverses one by one.
To the end. Reduceright () starts from the last entry of the array and traverses forward to the first entry.
Both methods receive two parameters: a function called on each item and (optional) the initial value of the basic value of the merge. Upload
The reduce () and reduceright () functions receive four parameters: the index of the previous value, current value, and item, and the array object. This
Any value returned by a function is automatically passed to the next parameter as the first parameter. The first iteration occurs in the second entry of the array.
One parameter is the first item of the array, and the second parameter is the second item of the array.
The reduce () method can be used to calculate the sum of all values in the array, for example:

VaR values = [1, 2, 4, 5]; var sum = values. reduce (function (prev, cur, index, array) {return Prev + cur;}); alert (SUM); // 15

 

S1: array Iteration Method

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.