Hi, I'm Mayone 👋

I'm a Web Developer and Entrepreneur

Tejiri Mayone

Hi, I'm Mayone 👋

I'm a Web Developer and Entrepreneur

The power of the Javascript reduce() function.

2 minutes
April 16, 2024
Javascript Reducer

Javascript Reducer

Introduction

The reduce() method is a powerful higher-order function in JavaScript that allows you to reduce an array to a single value by iteratively processing each element of the array. It takes a callback function as its first argument, and this function is executed on each element of the array, with the output being fed back into the next iteration of the function. The reduce() method also takes an optional second argument, which is the initial value of the accumulator.

The callback function passed to the reduce() method takes two arguments:

It is also possible to pass two additional optional arguments to the callback function:

  1. Index (optional): This is the index of the current element being processed.

  2. Array (optional): This is the original array that is being processed.

Here is an example of how to use reduce() to sum an array of numbers:

---
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((accumulator, currentValue) =>
{
  return accumulator + currentValue;
}, 0);

console.log(sum); // Output: 15
---

In this example, the reduce() method takes a callback function that adds the accumulator (which starts at 0) to the current value of each element of the array. The result of each iteration is passed as the accumulator to the next iteration, until all elements have been processed.

Here are a few more use cases for reduce():

const numbers = [1, 2, 3, 4, 5];
const max = numbers.reduce((accumulator, currentValue) => {
  return Math.max(accumulator, currentValue);
}, 0);

console.log(max); // Output: 5
const words = ['The', 'quick', 'brown', 'fox'];
const sentence = words.reduce((accumulator, currentValue) => {
  return `${accumulator} ${currentValue}`;
});

console.log(sentence); // Output: "The quick brown fox"
const people = [  { name: 'Alice', age: 25 },  { name: 'Bob', age: 30 },  { name: 'Charlie', age: 25 },  { name: 'Dave', age: 35 },];

const groupedByAge = people.reduce((accumulator, currentValue) => {
  const age = currentValue.age;
  if (!accumulator[age]) {
    accumulator[age] = [];
  }
  accumulator[age].push(currentValue);
  return accumulator;
}, {});

console.log(groupedByAge);
// Output: {25: [{name: "Alice", age: 25}, {name: "Charlie", age: 25}]}

Thanks for reading please don`t forget to like or share