Parse JavaScript array method reduce

Source: Internet
Author: User
This article mainly introduces the summary, syntax, parameters, and working principle of the reduce JavaScript array method, which is easy to understand. You can check Array. prototype. reduce ()

Overview

The reduce () method is an instance method of the array (a common method), which can be called by the Instance Object of the array. The reduce () method receives a function as an accumulator. Each value in the array (from left to right) is reduced to a value.

Syntax

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

Parameters

The callback function can pass four parameters.

Previusvalue: The value returned by the last callback function call, or the provided initial value (initialValue)

CurrentValue: The currently processed element in the array.

CurrentIndex: The index of the currently processed element in the array, that is, the index of currentValue. If there is an initial initialValue, it starts from 0. If it does not start from 1

Array: the array that calls reduce.

InitialValue: an optional parameter, which is the first parameter to call callback for the first time.

Return Value

The return value of reduce () is the result returned by the last callback function call.

Description

Reduce performs the callback function sequentially for each element in the array. It does not include the elements in the array that are deleted or never assigned a value. It accepts four parameters:

Previusvalu last value

Current currentValue

Index of the current currentIndex Value

Array

During the first execution of the callback function, previousValue and currentValue may be one of two different values. If reduce has the initialValue parameter, previusvalue is equal to initialValue and currentValue is equal to the first value in the array; if reduce does not have the initialValue parameter, previusvalue is equal to the first value in the array, and currentValue is equal to the second value in the array.

Note: If the initialValue parameter is not available, reduce executes the callback function starting from index 1 and skips the first index. If the initialValue parameter exists, reduce executes the callback from index 0.

If the array is empty and does not have the initialValue parameter, a TypeError is thrown. if the array has only one element and has no initial value initialValue, or initialValue but the array is empty, this unique value is directly returned without calling the callback function.

Generally, it is safer to provide an initial initialValue, because the following output result is displayed if no initialValue is provided.

// InitialValuefunction foo () {return [1, 2, 3, 4, 5] is not provided. reduce (x, y) => x + y); // 15}; console. log (foo. call (this); function foo () {return []. reduce (x, y) => x + y); // TypeError}; console. log (foo. call (this); // initialValuefunction 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. The parameters and returned values of each callback are as follows:

Finally, the return value (20) of the reduce function is returned as the result of the reduce function.

Note: If the initialValue parameter is added, the callback function will be called once more.

Example

Add all the items in the array

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

Flat 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 all the content of this article. I hope this article will help you in your study or work, and I also hope to support PHP!

For more articles on How to parse the JavaScript array method reduce, please follow the PHP Chinese network!

Related Article

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.