One way to find the sum of all values in a map in Typescript is by using the reduce()
method on the array of values of the map.
Here is an example of how to do it:
index.ts206 chars10 lines
The Array.from()
method is used to create an array of the values of the map, which can then be used with the reduce()
method. The reduce()
method takes a function that takes an accumulator value and the current value, and returns a new accumulator value. In this case, the accumulator starts at 0, and adds each value from the array of map values to it. The final result is the sum of all values in the map.
gistlibby LogSnag