JavaScript - examples of reduce () function

I am considering this example using the reduce () function.

function add(runningTotal, currentValue) { return runningTotal + currentValue; } var nums = [1,2,3,4,5,6,7,8,9,10]; var sum = nums.reduce(add); print(sum); // displays 55 

Could you show me some other examples of using reduce () - I'm not sure I fully understand how this works.

thanks

+6
source share
4 answers

As a result, the value of initialValue , a function with two important parameters decreases (may take more) and list values. If there is no initialValue , then he accepted it as the first element of the list. The function should do something with the previousValue , usually used as the accumulator, and nextValue .

So, suppose you have a list of values: [1, 2, 3, 4, 5] and the function should add 2 parameters and initialValue of 0 .

First step:

 0 + 1 = 1 2 3 4 5 

Second step:

 1 + 2 = 3 3 4 5 

Third step:

 3 + 3 = 6 4 5 

Fourth step:

 6 + 4 = 10 5 

Fifth step:

 10 + 5 = 15 //Final value 

As you can see, the input went from list to a single value, hence the name reduce . In your example, there is no initialValue (this is the second argument), so it seems to start in the second step.

+9
source

reduce() works by iterating over the array and calling the reducer function (this function is the first argument passed to reduce() . the function has four arguments:

  • previousValue , which is a kind of "current result". it is initially undefined unless you provide the initial value as the second argument to reduce() .
  • currentValue which is an object in your array
  • index current value in your array
  • array , the array itself

when you call the reducer function, its return value becomes the new argument of previousValue the next time the repeater function is called. what a magic. after calling the reducer function and getting the last object in the array, its return value will be the return value of reduce() .

Honestly, many of the examples you'll see (for example, using reduce() to summarize the chain) are pretty easy to do with a for() loop. its real value is when you perform functional programming .

+3
source

You can set the second argument to reduce

 function add(runningTotal, currentValue) { return runningTotal + currentValue; } var someInitValue = 10 var nums = [1,2,3,4,5,6,7,8,9,10]; var sum = nums.reduce(add, someInitValue); console.log(sum); // displays 65 
+2
source

You can also use shorthand to do things like generate some kind of stream cipher.

 var plaintext = "thisisaplaintext"; var chars = plaintext.split(''); var result = ''; function encrypt(runningTotal, currentValue){ var newVal = ((runningTotal + (""+runningTotal).charCodeAt()-32) % 94)+32 result = result + String.fromCharCode(newVal) return newVal; } chars.reduce(encrypt, 15 /*any arbitrary starting value*/); console.log(result); 

In principle, everything that can be done with a combination of independent calculations or anything that requires some total amount. This is nothing you could do with the for loop, but it looks cleaner.

+1
source

All Articles