LearnJavaScript Array Methods: reduce()

Dustin Usey
writes on December 13, 2022

The reduce() method in JavaScript is used to apply a function to each element in an array, with the purpose of reducing the array to a single value. This method is often used for tasks such as summing the elements of an array or calculating the average of the elements in an array.

Here is an example of using the reduce() method to sum the elements of an array:

const numbers = [1, 2, 3, 4, 5];

const sum = numbers.reduce((total, num) => total + num);

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

In this example, the reduce() method is called on the numbers array and is passed a callback function that takes two arguments: total and num. The total argument is the accumulator, which accumulates the result of the callback function as the reduce() method iterates over the elements of the array. The num argument is the current element being processed in the array.

In each iteration of the reduce() method, the callback function adds the current element (num) to the accumulator (total), and the result of this operation is stored in the total variable. This process continues until all elements in the array have been processed, and the final value of total is returned by the reduce() method.

In this example, the reduce() method is used to sum the elements of the numbers array, but it can be used for a variety of tasks involving the manipulation of arrays.

The reduce() method in JavaScript can be used to perform a variety of tasks involving the manipulation of arrays. Some common tasks that can be performed using the reduce() method include:

  • Summing the elements of an array
  • Calculating the average of the elements in an array
  • Flattening a multi-dimensional array into a single-dimensional array
  • Counting the occurrences of a particular element in an array
  • Converting an array of objects into an object
  • Filtering an array based on a condition

Here are some examples of using the reduce() method for these tasks:

  • Summing the elements of an array:
const numbers = [1, 2, 3, 4, 5];

const sum = numbers.reduce((total, num) => total + num);

console.log(sum); // Output: 15
  • Calculating the average of the elements in an array:
const numbers = [1, 2, 3, 4, 5];

const average = numbers.reduce((total, num) => total + num) / numbers.length;

console.log(average); // Output: 3
  • Flattening a multi-dimensional array into a single-dimensional array:
const numbers = [[1, 2], [3, 4], [5, 6]];

const flat = numbers.reduce((total, nums) => total.concat(nums), []);

console.log(flat); // Output: [1, 2, 3, 4, 5, 6]
  • Counting the occurrences of a particular element in an array:
const numbers = [1, 2, 3, 4, 5, 2, 3, 2];

const count = numbers.reduce((total, num) => {
  if (num === 2) {
    total++;
  }
  return total;
}, 0);

console.log(count); // Output: 3
  • Converting an array of objects into an object:
const people = [
  { name: "John", age: 30 },
  { name: "Jane", age: 28 },
  { name: "Jim", age: 32 }
];

const peopleObj = people.reduce((obj, person) => {
  obj[person.name] = person.age;
  return obj;
}, {});

console.log(peopleObj); // Output: { John: 30, Jane: 28, Jim: 32 }
  • Filtering an array based on a condition:
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const evens = numbers.reduce((total, num) => {
  if (num % 2 === 0) {
    total.push(num);
  }
  return total;
}, []);

console.log(evens); // Output: [2, 4, 6, 8, 10]

As you can see, the reduce() method in JavaScript is a versatile and powerful tool for working with arrays. It can be used to perform a wide range of tasks involving the manipulation of arrays, and is often a useful alternative to more traditional looping constructs such as for and while loops.

Leave a Reply

You must be logged in to post a comment.

Learning to code can be fun!

Get started today with a free trial and discover why thousands of students are choosing Treehouse to learn about web development, design, and business.

Learn more