Following is an array of hashes:
array = [
{loading: 10, avg: 15, total: 25 },
{loading: 20, avg: 20, total: 40 },
{loading: 30, avg: 25, total: 55 }
]
I need to sum the values of each hash and the result should be a hash of same keys:
{loading: 60, avg: 60, total: 120}
One way is to sum the values individually for each key and then merge in a hash like this:
loading = array.map { |h| h[:loading] }.sum
avg = array.map { |h| h[:avg] }.sum
total = array.map { |h| h[:total] }.sum
{loading: loading, avg: avg, total: total}
Is there a better way to do it? because the keys may increase in the future.