I want to sum the total values of the same items in an array.
I have a array as
[{"a"=>1},{"b"=>2},{"c"=>3},{"a"=>2},{"b"=>4}]
I want to get the result as
[{"a"=>3},{"b"=>6},{"c"=>3}]
Which method can do it?
array =[{"a"=>1},{"b"=>2},{"c"=>3},{"a"=>2},{"b"=>4}]
array.reduce({}) { |hash, acc| acc.merge(hash) {|_k, old, new| old + new } }
# => {"b"=>6, "a"=>3, "c"=>3}