Skip to main content

Command Palette

Search for a command to run...

.reduce method in JavaScript

Published
3 min readView as Markdown
.reduce method in JavaScript

Let’s start with .reduce inBuilt function.

The Reduce() method uses the reducer function that iterates through each element in the array and returns the single input

***Syntax

arr.reduce(callback(accumulator, currentValue), initialValue)***

Let’s see an example of Why reduce is used:

// Sum of the elements
let array = [10, 8, 15, 18, 9]
let result = 0
for(let i = 0;i<array.length ; i++){
              result += array[i]
    }
console.log(result)
//   output = 60

Explanation of code

  • Let’s take the array with 4 Elements

      //Here is an Array 
      let array = [10, 8 15 18, 9]
    

    Then Again FOR Loop, which iterates through the array and takes each array[i] value and push it into a new variable array by adding it to the result.

      let result = 0
      for(let i = 0;i<array.length ; i++){
                    result += array[i]
          }
      console.log(result)
      //   output = 60
    
  • Above Example, Here we iterate the array and add each element in a new variable. We used for loop to iterate in each and return the output.

  • But the coder becomes efficient when he solves the problem in a shorter and smart way

  • so, just can't we do it better? YES OR NO!!!!!

💡 YES!!!!! For better use and efficient work, Find was made as an inBuilt function. So, let’s see What it is!!!!

// // Find the number greater than 18
let array = [10, 8, 15, 18, 9]
const sum = (array) => array.reduce(function (previousValue, currentValue) {
    return previousValue + currentValue;
});
console.log(sum(array))
//   output = 60

Explanation of code

  • Let’s take the array with 4 Elements

      //Here is an Array 
      let array = [10, 8, 15, 18, 9]
    
    • When we use the reduce method,

    • We take a new variable function with Const then we pass the argument as Array.

    • Then .reduce takes the action of For loop and takes Array[i] as an argument with name num

    • Here again, in reduction, we need to give the parameters of accumulator and current value

    • Accumulator stores the return value of call back function

    • The current value is the an array of [i] th element

    • And then call back of function continues and gives the result

    const sum = (array) => array.reduce(function (previousValue, currentValue) {
        return previousValue + currentValue;
    });
    console.log(sum(array))
    //   output = 60

💡 YES!!!!! Just in one line, we have finished the array problem. By using reduced Function

https://media.tenor.com/kyk4ZXEgo-AAAAAC/get-out-of-here-throw.gif

Key Points of reduce() method

  • .reduce inBuilt Function accepts only function.

  • Either write the function at the same pace or just create a function and call back it from .reduce

  • This method accepts two parameters and 1 initial value

  • The initial value is not mandatory, While we iterate through the object if needed use it.

  • It helps to navigate easily

  • function(accumulator, currentValue, index)

    • currentValue*: It is a required parameter and it holds the value of the current element.*

    • index*: It is an optional parameter and it holds the index of the current element.*

    • accumulator*: Accumulator stores the return value of the call back function.*

  • Returns only 1 element as output

  • returns undefined when the array is empty or the condition is not satisfied

  • Not changes the original array

  • In objects, takes list of objects and destruct the object in function and use .find

💡 Can we use the objects????

Yes!!! we can but objects should be encoded within the List