How to Sum the Values of an Object in JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Sum the Values of an Object #

To sum the values of an object:

  1. Use the Object.values() method to get an array of the object's values.
  2. Use the reduce() method to iterate over the array.
  3. Add the current number to the accumulated number and return the result.
index.js
const obj = { one: 5, two: 15, three: 45, }; // 👇️ [5, 15, 45] const values = Object.values(obj); const sum = values.reduce((accumulator, value) => { return accumulator + value; }, 0); console.log(sum); // 👉️ 65

We used the Object.values method to get an array containing the object's values.

The initial value for the accumulator is 0 because that's what we passed as the second argument to the reduce method.

On each iteration, we increment the accumulator by the current value and return the result.

You can imagine that the accumulator argument gets set to whatever was returned from the previous invocation of the callback function.

The sum variable stores the sum of the object's values after the last iteration.

Alternatively, you can use a for...of loop to iterate over the array.

Sum the Values of an Object using for...of #

To sum the values of an object:

  1. Initialize a sum variable to 0 using the let keyword.
  2. Use the Object.values() method to get an array of the object's values.
  3. Use a for...of loop to iterate over the array and increment the sum variable by the current value.
index.js
const obj = { one: 5, two: 15, three: 45, }; let sum = 0; for (const value of Object.values(obj)) { sum += value; } console.log(sum); // 👉️ 65

Notice that we declare the sum variable using the let keyword. Had we used const, we wouldn't be able to reassign it.

We used the Object.values method to get an array of the object's values.

The for...of loop allows us to iterate over the array.

On each iteration, we increment the sum variable by the current value.

Alternatively, you can use a for...in loop.

Sum the Values of an Object using for...in #

To sum the values of an object:

  1. Initialize a sum variable to 0 using the let keyword.
  2. Use the for...in loop to iterate over the object's properties.
  3. On each iteration, increment the sum variable with the current value.
index.js
const obj = { one: 5, two: 15, three: 45, }; let sum = 0; for (const key in obj) { sum += obj[key]; } console.log(sum); // 👉️ 65

The for...in loop iterates over the object's properties and allows us to access each value.

Once the loop has iterated over all of the object's properties we have the sum of the values.

Which approach you pick is a matter of personal preference. I'd go with the reduce method because it doesn't require a declaration of an intermediary variable.
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.