Home > Web Front-end > JS Tutorial > What does the reduce() method in javascript do?

What does the reduce() method in javascript do?

青灯夜游
Release: 2021-10-19 15:42:43
Original
9777 people have browsed it

In JavaScript, the reduce() method is used to iterate (accumulate) array elements. It will call the specified callback function as an accumulator for all elements in the array. Each value in the array (from left to the right) begins to reduce and finally calculates to a value.

What does the reduce() method in javascript do?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In JavaScript, the reduce() method is used to iterate (accumulate) array elements. This method receives a function as an accumulator. Each value in the array (from left to right) begins to reduce, and finally Computes to a value.

The reduce() method can call the specified callback function for all elements in the array. The return value of this callback function is the cumulative result, and this return value is provided as a parameter the next time the callback function is called.

Syntax:

array.reduce(function callbackfn(previousValue, currentVaule, currentIndex, array), initialValue)
Copy after login

function callbackfn(previousValue, currentVaule, currentIndex, array): Required parameters, specify the callback function, at most Can receive 4 parameters:

  • previousValue: the value obtained by calling the callback function last time. If initialValue is provided to the reduce() method, the previousValue is initialValue when the function is first called.

  • currentValue: The value of the current element array.

  • currentIndex: The numeric index of the current array element.

  • array: Array object containing the element.

initialValue: Omissible parameter, initial value passed to the function.

Let’s learn more about it through code examples:

Example 1: Accumulate and sum array values

var a = [11, 12, 13], sum = 0;
function f(pre,curr) {
	sum=pre+curr;
	return sum;
}

a.reduce(f);
console.log(sum);
Copy after login

Output results:

36
Copy after login

Example 2: Concatenate array values ​​into strings

var a = [11, 12, 13], str = '';
function f(pre,curr) {
	str=pre+'-'+curr;
	return str;
}

a.reduce(f);
console.log(str);
Copy after login

What does the reduce() method in javascript do?

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of What does the reduce() method in javascript do?. For more information, please follow other related articles on 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