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 to find the sum of arrays

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "how to use JavaScript to find the sum of arrays". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Methods: 1, use "arr.reduce (function f (pre,curr) {return pre+cur})" statement; 2, use "arr.reduceRight (function f (pre,curr) {return pre+cur})".

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

Using JavaScript to find the sum of arrays

Method 1: use reduce ()

Reduce () evaluates the array element to a value (from left to right).

Var a = [1,2,3,4,5]; var b = a.reduce (function f (pre, curr) {return pre + curr;}); console.log (b)

Description:

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. The specific usage is as follows:

Array.reduce (callbackfn [, initialVaule])

Parameter description:

Array: required parameter, an array object.

Callbackfn: required arguments, a function that accepts up to four arguments. For each element in the array, the recude () method calls the callbackfn function once.

InitialVaule: optional parameter, if initialVaule is specified, it will be used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as a parameter rather than an array value.

The return value of the reduce () method is the cumulative result of the last call to the callback function.

If the argument initialVaule is provided, the reduce () method calls the callbackfn function once on each element in the array (in ascending indexing order), and if you provide initialVaule, the reduce () method calls the callbackfn function on each element starting with the second element.

The return value of the callback function is provided as the previousValue parameter the next time the callback function is called. The last call to the callback function returns the return value of the recude () method. This method does not call the callback function for missing elements in the array.

The syntax of the callback function is as follows:

Function callbackfn (previousValue, currentVaule, currentIndex, array)

Callback function parameter description:

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.

When the callback function is called for the first time, the value provided as a parameter depends on whether the reduce () method has an initialValue parameter. If you provide initialValue to the recude () method, the previousValue parameter is the initialValue,currentValue parameter is the value of the first element in the array.

Method 2: use reduceRight ()

ReduceRight () evaluates the array element to a value (from right to left).

Var arr = [1,2,3,4,5,5]; var b = arr.reduceRight (function f (pre, curr) {return pre + curr;}); console.log (b)

Description:

The reduceRight () method calls the specified callback function for all elements in the array from right to left. 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. The specific usage is as follows:

Array.reduceRight (callbackfn [, initialValue])

The syntax and usage of this method is roughly the same as that of the reduce () method, except that it calls the callback function starting at the right side of the array. If initialValue is provided, the reduceRight () method calls the callbackfn function once on each element in the array in descending order of index. If no initialValue is provided, the reduceRight () method calls the callbackfn function on each element, starting with the penultimate element, in descending indexing order.

"how to use JavaScript to find the sum of arrays" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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