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

What is the syntax of the JavaScript array reduce ()

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what is the syntax of JavaScript array reduce ()". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the syntax of the JavaScript array reduce ()"?

Preface

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

Reduce executes the callback function for each element in the array in turn, excluding elements that have been deleted or never assigned in the array, and accepts four parameters: the initial value (or the return value of the last callback function), the current element value, the current index, and the array that calls reduce.

1. Syntax array.reduce (function (prev, cur, index, arr), initialValue) / / abbreviated to facilitate interpretation of arr.reduce (callback, [initialValue])

Parameter meaning:

Callback (a function that executes each value in the array, containing four parameters)

1. PreviousValue (the value returned by the last callback, or the initial value provided (initialValue))

2. CurrentValue (the element currently being processed in the array)

3. Index (index of the current element in the array)

4. Array (an array that calls reduce)

InitialValue (as the first argument to call callback for the first time. ) is similar to setting the initial value

2. Parsing initialValue parameters with an example

Let's look at the first example:

Var arr = [1, 2, 3, 4]; var sum = arr.reduce (function (prev, cur, index, arr) {console.log (prev, cur, index); return prev + cur;}) console.log (arr, sum); / / print result: / / 1 21, 2, 3, 2, 10

As you can see here, the above example index starts at 1, and the value of the first prev is the first value of the array. The length of the array is 4, but the reduce function loops three times.

Let's look at the second example:

Var arr = [1, 2, 3, 4]; var sum = arr.reduce (function (prev, cur, index, arr) {console.log (prev, cur, index); return prev + cur;}, 0) / / Note that the initial value console.log (arr, sum) is set here; / / print result: / / 0 10 / / 1 2 1 amp 3 3, 2, 3, 4] 10

This example index starts at 0, the first value of prev is the initial value of 0 we set, and the length of the array is 4 times.

Conclusion: if initialValue,reduce is not provided, the callback method will be executed at index 1, skipping the first index. If initialValue is provided, start with index 0.

Note: if the array is empty, what happens with reduce?

Var arr = []; var sum = arr.reduce (function (prev, cur, index, arr) {console.log (prev, cur, index); return prev + cur;}) / / error, "TypeError: Reduce of empty array with no initial value"

But if we set the initial value, there will be no error, as follows:

Var arr = []; var sum = arr.reduce (function (prev, cur, index, arr) {console.log (prev, cur, index); return prev + cur;}, 0) console.log (arr, sum); / / [] 0

So generally speaking, it's safer for us to provide initial values.

3. Simple usage of reduce

Of course, the simplest thing is the sum and product of the arrays that we usually use.

Var arr = [1,2,3,4]; var sum = arr.reduce ((xpeny) = > xonomy) var mul = arr.reduce ((xmemy) = > xonomy) console.log (sum); / / summation, 10console.log (mul); / / Quadrature, advanced usage of 244 and reduce

(1) calculate the number of occurrences of each element in the array

Let names = ["Alice", "Bob", "Tiff", "Bruce", "Alice"]; let nameNum = names.reduce ((pre,cur) = > {if (cur in pre) {else {pre [cur] = 1} return pre}, {}) console.log (nameNum); / / {Alice: 2, Bob: 1, Tiff: 1, Bruce: 1}

(2) Array deduplication

Let arr = [1console.log 2 pre.includes 4 console.log 1] let newArr = arr.reduce ((pre,cur) = > {if (! pre.includes (cur)) {return pre.concat (cur)} else {return pre}}, [] console.log (newArr); / /

(3) convert two-dimensional array to one-dimensional array.

Let arr = [[0,1], [2,3], [4,5]] let newArr = arr.reduce ((pre,cur) = > {return pre.concat (cur)}, []) console.log (newArr); / / [0,1,2,3,4,5]

(3) convert multi-dimensional array to one-dimensional array.

Let arr = [[0,1], [2,3], [4, [5Magne6 cur 7]] const newArr = function (arr) {return arr.reduce ((pre,cur) = > pre.concat (Array.isArray (cur)? newArr (cur): cur), [])} console.log (newArr (arr)); / / [0,1,2,3,4,5]

(4) summation of attributes in an object

Var result = [{subject: "math", score: 10}, {subject: "chinese", score: 20}, {subject: "english", score: 30}]; var sum = result.reduce (function (prev, cur) {return cur.score + prev;}, 0) Console.log (sum) / / 60 at this point, I believe you have a deeper understanding of "what is the syntax of JavaScript array reduce ()". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Internet Technology

Wechat

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

12
Report