Reduce() Array Method in Javascript

The reduce() method is a higher-order function available in JavaScript arrays. It applies a provided callback function to each element of an array, reducing it to a single value. The callback function takes two parameters: the accumulator (acc) and the current element (curr).

Let’s first understand roles of acc (accumulator) and curr (current element) in the reduce() method:

  1. Accumulator (acc):
  2. The accumulator is a value that accumulates or stores the intermediate result during the iteration process. It can be of any data type (number, string, array, object, etc.). The value of the accumulator is retained and updated for each element in the array as the reduce() method progresses.

  3. Current Element (curr):
  4. The current element represents the current item being processed in the array during each iteration. It changes for each element of the array as the reduce() method traverses through it.

Now, let’s explore some examples to understand the acc and curr parameters:

Example 1: Summing an array of numbers using reduce():

const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((acc, curr) => acc + curr, 0);
console.log(sum); // Output: 15

In this example, the accumulator (acc) initially starts with a value of 0. For each element (curr) in the array, the callback function adds the current element to the accumulator. The updated value of the accumulator is then used in the next iteration. Ultimately, the sum of all elements in the array is obtained.

Example 2: Concatenating an array of strings using reduce():

const strings = ['Hello', ' ', 'World', '!'];
const concatenatedString = strings.reduce((acc, curr) => acc + curr, '');
console.log(concatenatedString); // Output: 'Hello World!'

In this example, the accumulator (acc) is an empty string (''). The callback function concatenates each current string element to the accumulator, resulting in the final concatenated string.

Example 3: Flattening a nested array using reduce():

const nestedArray = [[1, 2], [3, 4], [5, 6]];
const flattenedArray = nestedArray.reduce((acc, curr) => acc.concat(curr), []);
console.log(flattenedArray); // Output: [1, 2, 3, 4, 5, 6]

Here, the accumulator (acc) is an empty array ([]). The callback function uses the concat() method to combine the current array (curr) with the accumulator, resulting in a flattened array with all the elements combined.

acc represents the accumulator, which stores the intermediate result during the iteration process, while curr represents the current element being processed. The callback function uses these parameters to perform specific operations on the elements of an array and update the accumulator as needed. The reduce() method is powerful for aggregating values, performing calculations, or transforming data in an array-based context.

Don't Miss Out! Subscribe to Read Our Latest Blogs.

If you found this blog helpful, share it on social media.

Subscription form (#5)

Pin It on Pinterest

Scroll to Top