按多个键分组哈希值

时间:2013-06-23 18:40:51

标签: ruby enumerable

我想group_by多个键:orders,idx,account等。下面的代码是Ruby on Rails - Hash of Arrays, group by and sum by column name的修改版本。任何人都可以推荐一种方法来分组多个键并将多个值相加?

例如,在下面的代码中,我只对" orders"进行分组。我想对订单,idx和帐户进行分组。

group_hashes some_array, ["order","idx","account"] ["money","amt"]

VS

group_hashes some_array, "order", "money","amt" 

代码:

some_array=[{"idx"=>"1234", "account"=>"abde", "amt"=>"2", "money"=>"4.00", 
      "order"=>"00001"}, {"idx"=>"1235", "account"=>"abde",  "amt"=>"2" ,
      "money"=>"2.00", "order"=>"00001"}, {"idx"=>"1235", 
      "account"=>"abde", "amt"=>"2" , "money"=>"3.00", "order"=>"00002"}]  # => [{"idx"=>"1234", "account"=>"abde", "amt"=>"2", "money"=>"4.00", "order"=>"00001"}, {"idx"=>"1235", "account"=>"abde", "amt"=>"2", "money"=>"2.00", "order"=>"00001"}, {"idx"=>"1235", "account"...

#group_hashes arr, "order", "money"
def group_hashes arr, group_field, *sum_field
  arr.inject({}) do |res, h|
    (res[h[group_field]] ||= {}).merge!(h) do |key, oldval, newval|
        sum_field.include?(key) ? (oldval.to_f + newval.to_f).to_s : oldval  # => "1234", "abde", "4.0", "6.0", "00001"
    end                                                                      # => {"idx"=>"1234", "account"=>"abde", "amt"=>"2", "money"=>"4.00", "order"=>"00001"}, {"idx"=>"1234", "account"=>"abde", "amt"=>"4.0", "money"=>"6.0", "order"=>"00001"}, {"idx"=>"1235", "account"...
    res                                                                      # => {"00001"=>{"idx"=>"1234", "account"=>"abde", "amt"=>"2", "money"=>"4.00", "order"=>"00001"}}, {"00001"=>{"idx"=>"1234", "account"=>"abde", "amt"=>"4.0", "money"=>"6.0", "order"=>"00001"}}, {"0...
  end.values                                                                 # => [{"idx"=>"1234", "account"=>"abde", "amt"=>"4.0", "money"=>"6.0", "order"=>"00001"}, {"idx"=>"1235", "account"=>"abde", "amt"=>"2", "money"=>"3.00", "order"=>"00002"}]
end                                                                          # => nil

group_hashes some_array, "order", "money","amt"  # => [{"idx"=>"1234", "account"=>"abde", "amt"=>"4.0", "money"=>"6.0", "order"=>"00001"}, {"idx"=>"1235", "account"=>"abde", "amt"=>"2", "money"=>"3.00", "order"=>"00002"}]

输入的预期输出:

input =[  {"idx"=>"1234", "account"=>"abde", "amt"=>"2", "money"=>"4.00", "order"=>"00001"}, 
          {"idx"=>"1234", "account"=>"abde",  "amt"=>"2" ,"money"=>"2.00", "order"=>"00001"}, 
          {"idx"=>"1235", "account"=>"abde", "amt"=>"2" , "money"=>"3.00", "order"=>"00002"},
          {"idx"=>"1235", "account"=>"abde", "amt"=>"3", "money"=>"4.00", "order"=>"00002"},
          {"idx"=>"1234", "account"=>"ddd", "amt"=>"2", "money"=>"4.00", "order"=>"00003"},
          {"idx"=>"1234", "account"=>"ddd", "amt"=>"2", "money"=>"4.00", "order"=>"00003"}
        ]
output =[  {"idx"=>"1234", "account"=>"abde", "amt"=>"4", "money"=>"6.00", "order"=>"00001"}, 
          {"idx"=>"1235", "account"=>"abde", "amt"=>"5" , "money"=>"7.00", "order"=>"00002"},
          {"idx"=>"1234", "account"=>"ddd", "amt"=>"4", "money"=>"8.00", "order"=>"00003"},
        ] 

1 个答案:

答案 0 :(得分:7)

我会这样做:

def group_hashes arr, group_fields
  arr.group_by {|hash| hash.values_at(*group_fields).join ":" }.values.map do |grouped|
    grouped.inject do |merged, n|
      merged.merge(n) do |key, v1, v2|
        group_fields.include?(key) ? v1 : (v1.to_f + v2.to_f).to_s
      end
    end 
  end
end

您可以使用多个分隔符将多个密钥组合在一起进行分组,请参阅arr.group_by {|hash| hash.values_at(*group_fields).join ":" }

我想,你不需要sum_field,因为通常所有未被分组的字段都应该总结。