计算重复值ruby哈希

时间:2016-01-30 05:44:22

标签: arrays ruby hash

我的输入数据:

jQuery

我想将其转换为:

[
  {"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh"},
  {"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh"}, 
  {"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh"}, 
  {"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh"},  
  {"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh"}, 
  {"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh"}, 
  {"discount_code"=>"4567", "affiliate_name"=>"an"}, 
  {"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh"}
]

这个输入数据包含它出现次数的计数,没有重复值。

3 个答案:

答案 0 :(得分:6)

如果您的数组是arr

arr.group_by(&:itself).map { |h,v| h.merge("count"=>v.size) }
  #=> [{"discount_code"=>"LAKR", "count"=>7, "affiliate_name"=>"Jasbir Singh"},
  #    {"discount_code"=>"4567", "count"=>1, "affiliate_name"=>"Jasbir Singh"}]

步骤:

h = arr.group_by(&:itself)
  #=> {{"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh"}=>
  #      [{"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh"},
  #       {"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh"},
  #       {"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh"},
  #       {"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh"},
  #       {"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh"},
  #       {"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh"},
  #       {"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh"}],
  #    {"discount_code"=>"4567", "affiliate_name"=>"an"}=>
  #      [{"discount_code"=>"4567", "affiliate_name"=>"an"}]}
enum = h.map
  #=> #<Enumerator: {
  #    {"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh"}=>
  #      [{"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh"},
  #       ...
  #       {"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh"}],
  #    {"discount_code"=>"4567", "affiliate_name"=>"an"}=>
  #      [{"discount_code"=>"4567", "affiliate_name"=>"an"}]}:map>
enum.each { |h,v| h.merge("count"=>v.size) }
  #=> [{"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh", "count"=>7},
  #    {"discount_code"=>"4567", "affiliate_name"=>"an", "count"=>1}]

enum的第一个元素被传递给块并分配给块变量:

h,v = enum.next
  #=> [{"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh"},
  #    ...
  #    {"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh"}]]

h #=> {"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh"} 
v #=> [{"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh"},
  #    ...
  #    {"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh"}] 

并执行块计算:

h.merge("count"=>v.size)
  #=> h.merge("count"=>7)
  #=> {"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh", "count"=>7} 

其余的计算方法类似。

答案 1 :(得分:1)

arr.uniq.map{|i| i.merge({'count' => arr.count(i)})}

=> [{"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh", "count"=>7}
{"discount_code"=>"4567", "affiliate_name"=>"an", "count"=>1}]

答案 2 :(得分:0)

arr.each_with_object(Hash.new(0)) { |e, m| m[e] += 1 }.map { |k, v| k.merge("count" => v) }
# => [{"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh", "count"=>7}, {"discount_code"=>"4567", "affiliate_name"=>"an", "count"=>1}]