reduce method in javascript array of bjects

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:


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:


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:


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.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe