Get the Sum of all Values in a Map using JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
3 min

banner

# Get the Sum of all Values in a Map in JavaScript

To get the sum of all values in a Map:

  1. Declare a sum variable and initialize it to 0.
  2. Use the forEach() method to iterate over the Map.
  3. On each iteration, add the number to the sum, reassigning the variable.
index.js
const map1 = new Map([ ['key1', 1], ['key2', 2], ]); let sum = 0; map1.forEach(value => { sum += value; }); console.log(sum); // ๐Ÿ‘‰๏ธ 3

get the sum of all values in map

The code for this article is available on GitHub

The function we passed to the Map.forEach() method gets called for each element in the Map object.

The forEach() method returns undefined, so we need to declare a sum variable that stores the state.

Notice that we used the let keyword to declare the sum variable.

Variables declared using const cannot be reassigned.

We used the Map() constructor to create a Map object that has 2 key-value pairs.

The function we passed to the Map.forEach() method gets called with 3 arguments:

  • The value of the current iteration
  • The key of the current iteration
  • The Map object that is being iterated

On each iteration, we add the current value to the sum and reassign the variable.

index.js
const map1 = new Map([ ['key1', 1], ['key2', 2], ]); let sum = 0; map1.forEach(value => { sum += value; }); console.log(sum); // ๐Ÿ‘‰๏ธ 3

Once the function iterates over the entire Map object, we have the total stored in the sum variable.

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

# Get the Sum of all Values in a Map using a for...of loop

This is a four-step process:

  1. Declare a sum variable and initialize it to an empty array.
  2. Use the Map.values() method to get an iterator of the Map's values.
  3. Use a for...of loop to iterate over the iterator.
  4. Add each value to the sum variable.
index.js
const map1 = new Map([ ['key1', 1], ['key2', 2], ]); let sum = 0; for (const value of map1.values()) { sum += value; } console.log(sum); // ๐Ÿ‘‰๏ธ 3

get sum of all values in map using for of

The code for this article is available on GitHub

The Map.values() method returns an iterator object of the Map's values.

index.js
const map1 = new Map([ ['key1', 1], ['key2', 2], ]); // ๐Ÿ‘‡๏ธ [Map Iterator] { 1, 2 } console.log(map1.values());

We used a for...of loop to iterate over the iterator object.

On each iteration, we add the current value to the sum variable.

Alternatively, you can use the reduce() method.

# Get the Sum of all Values in a Map using reduce()

This is a three-step process:

  1. Use the Map.values() method to get an iterator of the Map's values.
  2. Use the Array.from() method to convert the iterator to an array.
  3. Use the Array.reduce() method to get the sum of the values.
index.js
const map1 = new Map([ ['key1', 1], ['key2', 2], ['key3', 3], ]); const values = Array.from(map1.values()); const sum = values.reduce((accumulator, current) => { return accumulator + current; }, 0); console.log(sum); // ๐Ÿ‘‰๏ธ 6

get sum of all values in map using reduce

The code for this article is available on GitHub

The Array.from() method creates a new, shallow-copied array from the provided iterable.

We converted the iterator to an array to be able to use the Array.reduce() method.

The second argument we passed to the callback function is the initial value for the accumulator variable (0 in our case).

On each iteration, we add the current Map value to the accumulated number and return the result.

The accumulated number gets passed to the callback function on each iteration until the sum of the values is calculated.

Which approach you pick is a matter of personal preference. I'd use the Map.forEach() method because I find it quite direct and intuitive.

# 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