Home > Web Front-end > JS Tutorial > Detailed explanation of the reduce method of arrays in JavaScript

Detailed explanation of the reduce method of arrays in JavaScript

高洛峰
Release: 2016-12-28 09:35:08
Original
1538 people have browsed it

Introduction

Let’s first take a look at the official overview of this method: The reduce() method receives a function as an accumulator (accumulator), and each value in the array (from left to right) begins to reduce, Finally a value.

You must be a little confused like me. In fact, what reduce receives is a callback function to call each item in the array until the end of the array.

Let’s give an example and everyone will understand it clearly.

Suppose I have a string of arrays, and the arrays are all numbers. I want to calculate the sum of these numbers. Normally we would loop and add one by one. With reduce, we don’t have to go through so much trouble. We only use one line of code.

var total = [0,1,2,3,4].reduce((a, b)=>a + b); //10
Copy after login

How does this method work?

reduce accepts a function, which has four parameters, namely:

1. Last value;

2. Current value;

3. Index of the current value;

4. Array;

Let’s take the above array as an example and print out these parameters to see:

[0, 1, 2, 3, 4].reduce(function(previousValue, currentValue, index, array){
 return previousValue + currentValue;
});
Copy after login

The result obtained is:

Detailed explanation of the reduce method of arrays in JavaScript

Analyzing this result, this callback function was called a total of 4 times, because the first time there was no previousValue, so it starts directly from the second item of the array and is called to the end of the array.

reduce also has a second parameter. We can use this parameter as the first parameter when calling callback for the first time. In the above example, because there is no second parameter, it is directly obtained from the second item of the array. At the beginning, if we give the second parameter 5, then the result is like this:

[0, 1, 2, 3, 4].reduce(function(previousValue, currentValue, index, array){
 return previousValue + currentValue;
},5);
Copy after login

The value of previousValue for the first call is passed in Instead of the second parameter, the function is called 5 times, which is the length of the array.

Reduce can help us accomplish many things easily. In addition to accumulation, it also flattens a two-dimensional array:

var flattened = [[0, 1], [2, 3], [4, 5]].reduce(function(a, b) {
 return a.concat(b);
}, []);
// flattened == [0, 1, 2, 3, 4, 5]
Copy after login

##Summary

These are the two commonly used ones I can think of for now, but there must be many places where they can be used. The above is the entire content of this article. I hope the content of this article can be of some help to everyone's study or work. If you have any questions, you can leave a message to communicate.

For more detailed articles on the reduce method of arrays in JavaScript, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template