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 JavaScript's reduce ()

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

Share

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

This article mainly explains "how to use JavaScript's reduce ()". 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 JavaScript's reduce ()".

Case study

Calculate the sum of the added elements of the array:

Varnumbers= [65,44,12,4]

FunctiongetSum (total,num) {

Returntotal+num

}

FunctionmyFunction (item) {

Document.getElementById ("demo") [xss_clean] = numbers.reduce (getSum)

}

Output result:

one hundred and twenty five

Definition and usage

The reduce () method takes a function as an accumulator, and each value in the array (from left to right) starts to shrink and eventually evaluates to a value.

Reduce () can be used as a higher-order function for the compose of a function.

Note: reduce () does not execute callback functions for empty arrays.

Browser support

The numbers in the table represent the first browser version number that supports the method.

Method

Reduce () Yes 9.03.04 10.5

Grammar.

Array.reduce (function (total,currentValue,currentIndex,arr), initialValue)

Parameters.

Parameter description

Function (total,currentValue,index,arr) is required. The function used to execute each array element.

Function parameters:

Parameter description

Total is required. The initial value, or the return value after the calculation is finished.

CurrentValue is required. Current element

CurrentIndex is optional. Index of the current element

Arr is optional. The array object to which the current element belongs.

InitialValue is optional. The initial value passed to the function

Technical details

Return value: returns the calculation result

JavaScript version: ECMAScript3

Thank you for your reading, the above is the content of "how to use JavaScript's reduce ()". After the study of this article, I believe you have a deeper understanding of how to use JavaScript's reduce (), 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