Parsing JavaScript array Methods Reduce_javascript tips

Source: Internet
Author: User

Array.prototype.reduce ()

Overview

The reduce () method is an instance method of an array (a common method) that can be invoked by an instance object of an array. The reduce () method receives a function as an accumulator (accumulator), and each value in the array begins to shrink from left to right, eventually to a value.

Grammar

arr.reduce(callback[, initialValue]) {}

Parameters

Four parameters can be passed in a callback function.

previousvalue: The value returned by the last call to the callback function, or the supplied initial value (InitialValue)

CurrentValue: The element currently being processed in the array

currentindex: The index of the currently processed element in an array, that is, the currentvalue. If there is a InitialValue initial value, starting from 0. If not starting from 1

array: Calls to reduce arrays

initialvalue: Optional parameter, as the first argument for the first call to callback

return value

The reduce () return value is the result of the last call to the callback function

Describe

Reduce each element in the array to execute the callback function in turn, excluding elements that are deleted or never assigned in the arrays, and accept four parameters:

    1. Previousvalu Last Value
    2. CurrentValue Current Value
    3. Currentindex Index of current value
    4. Array arrays

The first time a callback function executes , Previousvalue and CurrentValue may be one of two different values, and if reduce has initialvalue parameters, then Previousvalue equals InitialValue, and CurrentValue equals the first value in the array, and if reduce does not have initialvalue parameters, then Previousvalue equals the first value in the array. CurrentValue equals the second value in the array.

Note : If there is no initialvalue parameter, reduce starts with the callback function from index 1 and skips the first index. If there is a initialvalue parameter, reduce starts the callback from index 0

If the array is empty and there is no initialvalue parameter, a TypeError error is thrown. If the array has only one element and no initial value initialvalue, or InitialValue but the array is empty, the unique value is returned directly without invoking the callback function.

Generally, it is safe to provide a InitialValue initial value, as the following output will appear if not.

InitialValue
function foo () {return
 [1,2,3,4,5].reduce ((x, y) => x + y) is not provided;//15
};
Console.log (Foo.call (this));
function foo () {return
 [].reduce ((x, y) => x + y);//typeerror
};
Console.log (Foo.call (this));
Provides initialvalue
function foo () {return
 [].reduce ((x, y) => x + y, 0);//0
};
Console.log (Foo.call (this));

How reduce () works

[0, 1, 2, 3, 4].reduce (Previousvalue, CurrentValue, index, array) => Previousvalue + currentvalue);

The callback is executed four times, and each parameter and return value is the following table:

The return value of reduce is the return value (10) of the last invocation of the callback function.

If you pass the initial value as the second parameter to reduce, the result is as follows:

[0, 1, 2, 3, 4].reduce (Previousvalue, CurrentValue, index, array) => Previousvalue + CurrentValue, 10);

The return value of the last function call (20) is returned as the result of the reduce function

Note: Adding the InitialValue parameter calls the callback function more than once

Examples

Add all items to an array

let sum = [0, 1, 2, 3, 4].reduce ((x, y) => x + y, 0); 
10

Flatten a two-dimensional array

Let arr = [[1, 2], [3, 4], [5, 6]].reduce ((x, y) => x.concat (y), []);
[1, 2, 3, 4, 5, 6]

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, but also hope that a lot of support cloud Habitat community!

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.