I have an array of hashes and would like to summarize the selected values. I know how to summarize them or one of them, but not how to select more than one key.
i.e:.
[{"a"=>5, "b"=>10, "active"=>"yes"}, {"a"=>5, "b"=>10, "active"=>"no"}, {"a"=>5, "b"=>10, "action"=>"yes"}]
To summarize all of them, I use:
t = h.inject{|memo, el| memo.merge( el ){|k, old_v, new_v| old_v + new_v}}
=> {"a"=>15, "b"=>30, "active"=>"yesnoyes"}
To summarize one key, I do:
h.map{|x| x['a']}.reduce(:+)
=> 15
How can I sum the values ββfor the keys "a" and "b"?
source
share