简体   繁体   English

带哈希的数组,如何合并相同的键并添加其值

[英]Array with hash, how to merge same keys and add its value

I have an array with hashes in it. 我有一个带有哈希的数组。 If they have the same key I just want to add its value. 如果它们具有相同的键,我只想添加其值。

@receivers << result

@receivers
=> [{:email=>"user_02@yorlook.com", :amount=>10.00}]
result
=> {:email=>"user_02@yorlook.com", :amount=>7.00}

I want the result of above to look like this 我希望以上结果看起来像这样

[{:email=>"user_02@yorlook.com", :amount=>17.00}]

Does anyone know how to do this? 有谁知道如何做到这一点?

Here is the the entire method 这是整个方法

  def receivers
    @receivers = []
    orders.each do |order|
      product_email = order.product.user.paypal_email
      outfit_email  = order.outfit_user.paypal_email
      if order.user_owns_outfit?
        result = { email: product_email, amount: amount(order.total_price) }
      else
        result = { email: product_email, amount: amount(order.total_price, 0.9),
                   email: outfit_email,  amount: amount(order.total_price, 0.1) }
      end
      @receivers << result
    end
  end

Using Enumerable#group_by 使用Enumerable#group_by

@receivers.group_by {|h| h[:email]}.map do |k, v|
  {email: k, amount: v.inject(0){|s,h| s + h[:amount] } }
end
# => [{:email=>"user_02@yorlook.com", :amount=>17.0}]

Using Enumerable#each_with_object 使用Enumerable#each_with_object

@receivers.each_with_object(Hash.new(0)) {|h, nh| nh[h[:email]]+= h[:amount] }.map do |k, v|
 {email: k, amount: v}
end
# Output: [{ "em@il.one" => 29.0 }, { "em@il.two" => 39.0 }]
def receivers
  return @receivers if @receivers
  # Produces: { "em@il.one" => 29.0, "em@il.two" => 39.0 }
  partial_result = orders.reduce Hash.new(0.00) do |result, order|
    product_email = order.product.user.paypal_email
    outfit_email  = order.outfit_user.paypal_email

    if order.user_owns_outfit?
      result[product_email] += amount(order.total_price)
    else
      result[product_email] += amount(order.total_price, .9)
      result[outfit_email]  += amount(order.total_price, .1)
    end

    result
  end

  @receivers = partial_result.reduce [] do |result, (email, amount)|
    result << { email => amount }
  end
end

I would just write the code this way: 我只是这样写代码:

def add(destination, source)
  if destination.nil?
    return nil
  end
  if source.class == Hash
    source = [source]
  end
  for item in source
    target = destination.find {|d| d[:email] == item[:email]}
    if target.nil?
      destination << item
    else
      target[:amount] += item[:amount]
    end
  end
  destination
end

usage: 用法:

@receivers = []
add(@receivers, {:email=>"user_02@yorlook.com", :amount=>10.00})
=> [{:email=>"user_02@yorlook.com", :amount=>10.0}]
add(@receivers, @receivers)
=> [{:email=>"user_02@yorlook.com", :amount=>20.0}]
a = [
  {:email=>"user_02@yorlook.com", :amount=>10.0},
  {:email=>"user_02@yorlook.com", :amount=>7.0}
]

a.group_by { |v| v.delete :email } # group by emails
 .map { |k, v| [k, v.inject(0) { |memo, a| memo + a[:amount] } ] } # sum amounts
 .map { |e| %i|email amount|.zip e } # zip to keys
 .map &:to_h # convert nested arrays to hashes

From what I understand, you could get away with just .inject : 据我了解,您可以只使用.inject

a = [{:email=>"user_02@yorlook.com", :amount=>10.00}]
b = {:email=>"user_02@yorlook.com", :amount=>7.00}
c = {email: 'user_03@yorlook.com', amount: 10}

[a, b, c].flatten.inject({}) do |a, e|
  a[e[:email]] ||= 0
  a[e[:email]] += e[:amount]
  a
end

=> {
  "user_02@yorlook.com" => 17.0,
  "user_03@yorlook.com" => 10
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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