Skip to content
the poor coder

reduce method in javascript array of bjects

Poor Man
Published
Reading time
2 min read
On this page

Reducing an Array of Objects in JavaScript Using the Reduce Method#

If you have an array of objects in JavaScript and want to perform some operation on them, you can use the reduce method. The reduce method is used to iterate over an array and perform some operation on each element, then return a single value. We can use this method to perform operations like summing up the values of an array, finding the average value, and so on.

How the Reduce Method Works#

The reduce method takes two arguments: a callback function and an initial value. The callback function takes two arguments: an accumulator and the current value being processed. The accumulator is the value returned by the previous iteration of the callback function, and the current value is the current element being processed.

The callback function can also take two additional arguments: the index of the current element and the array itself.

The reduce method iterates over the array and applies the callback function to each element, with the initial value being passed as the first argument to the first iteration. The final value returned by the reduce method is the accumulated result of all iterations.

Example#

Let's say we have an array of objects representing a list of items:

Text

const items = [
  { name: 'item1', price: 10 },
  { name: 'item2', price: 20 },
  { name: 'item3', price: 30 }
];

We want to calculate the total price of all items in the list. We can do this using the reduce method:

Text

const totalPrice = items.reduce((accumulator, currentItem) => {
  return accumulator + currentItem.price;
}, 0);

console.log(totalPrice); // Output: 60

In the above code, we pass a callback function to the reduce method. The callback function takes two arguments: the accumulator and the current item being processed. We add the price of the current item to the accumulator and return the result. The initial value of the accumulator is set to 0.

The reduce method iterates over the array and applies the callback function to each element. The result of each iteration is stored in the accumulator. Finally, the accumulated value is returned as the final result.

Alternative Approach#

We can also achieve the same result using the forEach method:

Text

let totalPrice = 0;

items.forEach((item) => {
  totalPrice += item.price;
});

console.log(totalPrice); // Output: 60

In this approach, we use the forEach method to loop over each item in the array and add its price to a variable called totalPrice. The final value of totalPrice is the total price of all items.

Conclusion#

The reduce method is a powerful tool for processing arrays of objects in JavaScript. It allows us to perform complex operations on an array and return a single value. We can use it to perform operations like summing up values, finding the average, filtering elements, and more.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC