How to Sum the Values of an Object in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 2, 2024
3 min

banner

# 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 Array.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

sum values of object

The code for this article is available on GitHub

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

index.js
const obj = { one: 5, two: 15, three: 45, }; const values = Object.values(obj); // ๐Ÿ‘‡๏ธ [ 5, 15, 45 ] console.log(values);

The function we passed to the Array.reduce() method gets called for each element in the array.

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

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
On each iteration, we sum the accumulator and 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.

If you have to calculate the sum of an object's values often, define a reusable function.

index.js
function sumObjectValues(object) { return Object.values(object).reduce((accumulator, value) => { return accumulator + value; }, 0); } const obj = { one: 5, two: 15, three: 45, }; const result = sumObjectValues(obj); console.log(result); // ๐Ÿ‘‰๏ธ 65

The sumObjectValues() function takes an object and calculates the sum of the object's values.

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

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

This is a four-step process:

  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.
  4. 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

sum values of object using for of

The code for this article is available on GitHub

Notice that we declared 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 statement is used to loop over iterable objects like arrays, strings, Map, Set and NodeList objects and generators.

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

If you have to do this often, extract the logic into a reusable function.

index.js
function sumObjectValues(object) { let total = 0; for (const value of Object.values(object)) { total += value; } return total; } const obj = { one: 5, two: 15, three: 45, }; const result = sumObjectValues(obj); console.log(result); // ๐Ÿ‘‰๏ธ 65

The sumObjectValues() function takes an object as a parameter, sums the values of the object and returns the result.

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

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

This is a three-step process:

  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

sum values of object using for in

The code for this article is available on GitHub

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 and is quite readable and direct.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev