简体   繁体   中英

Ruby combine array of hashes with the same key and multiple values

I have the following array which is actually a combination of two arrays. My goal is that the 2 hashes with employeeId of 898989 could be combined and that I could add their counts together and change their type to both. I tried the code below which is close to what I want, however I lose the other values of my hashes. Is there any easy way to map all of the values and do manipulation like adding the counts like I want?

combined = [{"@rid"=>"#-2:1", "employeeId"=>   "898989", "count"=>1, :type=>"wiki"  },
       {"@rid"=>"#-2:3", "employeeId"=>  "2423213", "count"=>7, :type=>"search"},
       {"@rid"=>"#-2:2", "employeeId"=>   "555555", "count"=>2, :type=>"search"},
       {"@rid"=>"#-2:5", "employeeId"=>   "898989", "count"=>2, :type=>"search"},
       {"@rid"=>"#-2:1", "employeeId"=>  "5453454", "count"=>1, :type=>"search"},
       {"@rid"=>"#-2:4", "employeeId"=>"987654321", "count"=>1, :type=>"search"}]

merged_array_hash = combined.group_by { |h1| h1["employeeId"] }.map do |k,v|
    { "employeeId" => k, :types =>  v.map { |h2| h2[:type] }.join(", ") }
end

merged_array_hash ends up being:

[{employeeId: "898989",types: "wiki, search"},
{employeeId: "2423213",types: "search"},
{employeeId: "555555",types: "search"},
{employeeId: "5453454",types:"search"},
{employeeId: "987654321",types: "search"}]

What I'm looking to get:

[{employeeId: "898989",type: "both", count: 2},
{employeeId: "2423213",type: "search", count: 7},
{employeeId: "555555",type: "search", count: 2},
{employeeId: "5453454",type:"search", count: 1},
{employeeId: "987654321",type: "search", count: 1}]

Not beautiful, but it will get the job done:

combined.group_by { |h1| h1["employeeId"] }.map do |k,v|
  types = v.map { |h2| h2[:type] }  
  count = v.sum { |x| x['count'] } 

  { employeeId: k, 
    types: (types.length == 1 ? types[0] : 'both'), 
    count: count }  

end  

=> [{:employeeId =>"898989", :types=>"both", :count=>3},
    {:employeeId =>"2423213", :types=>"search", :count=>7},
    {:employeeId =>"555555", :types=>"search", :count=>2},
    {:employeeId =>"5453454", :types=>"search", :count=>1},
    {:employeeId =>"987654321", :types=>"search", :count=>1}]

Also not beautiful, will also get the job done, potentially more readable

hash = {}
combined.each do |h|
  employeeId, count, type = h.values_at("employeeId", "count", :type)
  if hash.include? employeeId
    hash[employeeId][:count] += count
    hash[employeeId][:type] = "both"  #assumes duplicates can only occur if item is in both lists
  else
    hash[employeeId] = { :employeeId => employeeId, :type => type, :count => count }
  end
end
hash.values

Testing:

[{:employeeId=>"898989", :type=>"both", :count=>3},
{:employeeId=>"2423213", :type=>"search", :count=>7},
{:employeeId=>"555555", :type=>"search", :count=>2},
{:employeeId=>"5453454", :type=>"search", :count=>1},
{:employeeId=>"987654321", :type=>"search", :count=>1}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM