算上重复值红宝石哈希 [英] count repeated value ruby hash

查看:123
本文介绍了算上重复值红宝石哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的输入数据:

[
  {"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"}
]

我想将它转换成这样:

I want to transform it to this:

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

这具有的时候,它出现的数目的计数的输入数据,并没有重复值。

This has the input data with the count of the number of times it appeared, and no repeat values.

推荐答案

如果改编是数组:

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}]

枚举被传递到块和分配给该块的变量的第一个元素

The first element of enum is passed to the block and assigned to the block variables:

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} 

剩余的计算是相似的。

The remaining calculations are similar.

这篇关于算上重复值红宝石哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆