Last updated: Mar 3, 2024
Reading timeยท3 min

To get the sum of all values in a Map:
sum variable and initialize it to 0.forEach() method to iterate over the Map.sum, reassigning the variable.const map1 = new Map([ ['key1', 1], ['key2', 2], ]); let sum = 0; map1.forEach(value => { sum += value; }); console.log(sum); // ๐๏ธ 3

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.
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:
value of the current iterationkey of the current iterationMap object that is being iteratedOn each iteration, we add the current value to the sum and reassign the
variable.
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.
for...of loopThis is a four-step process:
sum variable and initialize it to an empty array.Map.values() method to get an iterator of the Map's values.for...of loop to iterate over the iterator.sum variable.const map1 = new Map([ ['key1', 1], ['key2', 2], ]); let sum = 0; for (const value of map1.values()) { sum += value; } console.log(sum); // ๐๏ธ 3

The Map.values() method returns an iterator object of the Map's values.
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.
reduce()This is a three-step process:
Map.values() method to get an iterator of the Map's values.Array.from() method to convert the iterator to an array.Array.reduce() method to get the sum of the values.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

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.
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.
You can learn more about the related topics by checking out the following tutorials: