你好我有一个类似于这个
的散列@receivers
=> [{:amount=>50, :email=>"user_02@example.com"},
{:amount=>50, :email=>"user_02@example.com"},
{:amount=>50, :email=>"user_02@example.com"},
{:amount=>100, :email=>"user_01@example.com"},
{:amount=>100, :email=>"user_01@example.com"}]
我如何让它看起来像这样?:
@receivers
=> [{:amount=>150, :email=>"user_02@example.com"}
{:amount=>200, :email=>"user_01@example.com"}]
感谢您的帮助。
答案 0 :(得分:3)
您可以这样计算:
@receivers.group_by { |e| e[:email] }
.map { |k, v| { amount: v.sum { |e| e[:amount] }, email: k } }
#=> [{:amount=>150, :email=>"user_02@example.com"},
# {:amount=>200, :email=>"user_01@example.com"}]