Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the reduce () method in javascript

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use the reduce () method in javascript". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the reduce () method in javascript".

In javascript, the reduce () method is used to iterate (accumulate) the array elements, calling the specified callback function as an accumulator on all elements in the array, and each value in the array (from left to right) starts to shrink and finally evaluates to a value.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

In javascript, the reduce () method is used to iterate (accumulate) over array elements, which takes a function as an accumulator, and each value in the array (from left to right) starts to shrink and is eventually evaluated as a value.

The reduce () method calls the specified callback function on all elements in the array. The return value of the 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 (previousValue, currentVaule, currentIndex, array), initialValue)

Function callbackfn (previousValue, currentVaule, currentIndex, array): required parameters. Specify callback function. You can receive up to 4 parameters:

PreviousValue: the value obtained from the last callback function call. If you provide initialValue to the reduce () method, previousValue is initialValue when the function is first called.

CurrentVaule: the value of the current element array.

CurrentIndex: the numeric index of the current array elements.

Array: the array object that contains this element.

InitialValue: the parameter that can be omitted, the initial value passed to the function.

Let's take a look at the code example:

Example 1: accumulating array values

Var a = [11,12,13], sum= 0 sum=pre+curr; return sum; function f (pre,curr) {sum=pre+curr; return sum;} a.reduce (f); console.log (sum)

Output result:

thirty-six

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)

Thank you for reading, the above is the content of "how to use the reduce () method in javascript". After the study of this article, I believe you have a deeper understanding of how to use the reduce () method in javascript, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report