How To Sum Total From Array Of Object Properties With JavaScript Reduce Method
Published: 2022-12-20 15:42:54

---
title: How To Sum Total From Array Of Object Properties With JavaScript Reduce Method
published: true
date: 2022-12-20 08:44:08 UTC
tags: 
canonical_url: https://byrayray.dev/posts/2022-12-19-sum-total-array-object-properties-javascript-reduce-method
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m2dfzuzyzfbaub3ugzuq.png
---

Calculating the total price for your shopping cart was a hassle in the days before the JavaScript `reduce` method. In this post, I will show you how easy it is to use the `reduce` method to calculate the total price of a product array in the shopping cart. The JavaScript `reduce` method is powerful and can calculate a total price based on an array of object properties.

![divider-byrayray.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629890886208/NhHYvPmBA.png)

## How does the reduce method work?

The **`reduce()`** method in JavaScript is used to execute a function to each element in an array, resulting in a single output value. It operates on an array of elements, performing a computation on each element in the array and building up the result.

Here is the syntax for using the **`reduce()`** method:

```js
array.reduce((accumulator, currentValue, currentIndex, array) => {
  // code to be executed
}, initialValue);

```

The **`reduce()`** method takes in a callback function as its first argument. This callback function is called for each element in the array and takes four parameters:

- **`accumulator`** : This is the value returned in the final iteration. It starts with the initial value if provided or the first element of the array if no initial value is provided. And adds up every iteration.
- **`currentValue`** : This is the current element being processed in the array.
- **`currentIndex`** (_optional_): This is the index of the current item that is being processed in the array.
- **`array`** (_optional_): This is the array **`reduce()`** that was called upon.

The callback function should return the updated value for the accumulator.

Here is a simple example of using **`reduce()`** to calculate all the numbers in an array:

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

const sum = numbers.reduce((accumulator, currentValue) => {
  return accumulator + currentValue;
}, 0);

console.log(sum); // 15

```

Check [runkit example](https://runkit.com/devbyrayray/calculate-total-based-on-numbers-array-with-javascript-reduce)

In this example, the **`reduce()`** method is called on the **`numbers`** array and is passed a callback function that adds the accumulator and the current value. The initial value for the accumulator is 0.

The **`reduce()`** method iterates through the numbers in the array, starting with the first element, and applies the callback function to each element.

The returned value from the callback function becomes the new value for the accumulator, which is passed to the next iteration. This process continues until all elements in the array have been processed and the final value of the accumulator is returned.

Let's check how we can apply this to our product array of a fictional shopping cart.

![divider-byrayray.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629890886208/NhHYvPmBA.png)

## Get a total price from an array of object properties

Let's say you have an array of products in your shopping cart.

```js
const products = [
    {
      "price": 10,
      "title": "Item 1"
    },
    {
      "price": 20,
      "title": "Item 2"
    },
    {
      "price": 30,
      "title": "Item 3"
    }
]

```

To calculate the prices, we first need an array of prices. We want to calculate the total price of our shopping cart. We can do that by using the `.map()` method. In the example below, we create a new array with only the prices of our products.

```js
const prices = shoppingCart.map((product) => product.price)

```

After that, we can use that array to calculate the total price with the `reduce` method.

```js
const prices = shoppingCart.map((product) => product.price)
const total = prices.reduce((acc, curr) => acc + curr)

console.log('total: ', total )

```

We can also write this shorter and chain the `map()` and `reduce()` methods.

```js
const totalPrice = shoppingCart.reduce((acc, curr) => acc + curr.price, 0)

console.log('total: ', totalPrice )

```

Check [runkit example](https://runkit.com/devbyrayray/calculate-total-price-from-javascript-array-of-product-objects)

That was easy. 👍

![divider-byrayray.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629890886208/NhHYvPmBA.png)

## Thanks!

![hashnode-footer.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629789655319/nBF6anHH4w.png)

_After reading this story, I hope you learned something new or are inspired to create something new! 🤗 If so, consider subscribing via email (scroll to the top of this page) or follow me here on Hashnode._

> Did you know that you can create a [Developer blog like this one, yourself](https://hashnode.com/@devbyrayray/joinme)? It's entirely for free. 👍💰🎉🥳🔥

_If I left you with questions or something to say as a response, scroll down and type me a message. Please send me a_ [_DM on Twitter @DevByRayRay_](https://twitter.com/@devbyrayray) _when you want to keep it private. My DM's are always open 😁_