Gidrek Gidrek - 1 month ago 8
Ruby Question

Merge an array of hashes in Ruby and count the same key

I have some data in this format

[{
"_id" => "20",
"value" => 1
}, {
"_id" => "19",
"value" => 1
}, {
"_id" => nil,
"value" => 8
}, {
"_id" => "27",
"value" => 1
}, {
"_id" => "25",
"value" => 3
}, {
"_id" => "28",
"value" => 1
}]


I want to merge the same values with "_id" key and sum the "value" values.

Example of output

[{
"_id" => "20",
"value" => 1
}, {
"_id" => "19",
"value" => 2
}, {
"_id" => nil,
"value" => 8
}, ...]


There is a elegant way to do this?

I have tried with two loops but I think that is not the best way to do it.

Answer

As with most things in Ruby, a trip to the Enumerable documentation turns up the group_by method which can help group things together by some arbitrary criteria. Combine that with something that does the sums and you get this:

v.group_by do |e|
  e['_id']
end.map do |id, list|
  {
    '_id' => id,
    'value' => list.inject(0) { |sum, e| sum + e['value'] }
  }
end

# => [{"_id"=>"20", "value"=>1}, {"_id"=>"19", "value"=>2}, {"_id"=>nil, "value"=>28},
#     {"_id"=>"27", "value"=>1}, {"_id"=>"25", "value"=>3}, {"_id"=>"28", "value"=>1},
#     {"_id"=>"23", "value"=>1}, {"_id"=>"16", "value"=>1}, {"_id"=>"18", "value"=>2},
#     {"_id"=>"22", "value"=>2}]