如何通过其中一个哈希键值合并两个哈希数组?

时间:2013-07-09 18:49:16

标签: ruby

我有两个哈希数组:

[{:status=>"failed", :tag=>"tag156", :node=>"isfw-a"},
{:status=>"unchanged", :tag=>"tag156", :node=>"ayfw-a"},
{:status=>"changed", :tag=>"tag156", :node=>"vpfw-a"}]

[{:status=>"success", :sender=>"ayfw-a"},
{:status=>"success", :sender=>"vpfw-a"}]

我可以用来合并的密钥是:node:sender。 我需要将:status参数从第二个数组添加到第一个数组:another_status,将“skipped”放在第二个数组中没有对应的:sender的位置,如下所示:

[{:status=>"failed", :tag=>"tag156", :node=>"isfw-a", :another_status=>"skipped"},
{:status=>"unchanged", :tag=>"tag156", :node=>"ayfw-a", :another_status=>"success"},
{:status=>"changed", :tag=>"tag156", :node=>"vpfw-a", :another_status=>"success"}]

我无法想出实现这个目标的解决方案。

3 个答案:

答案 0 :(得分:2)

或许这样的事情?

a1 = [
  {:status=>"failed", :tag=>"tag156", :node=>"isfw-a"},
  {:status=>"unchanged", :tag=>"tag156", :node=>"ayfw-a"},
  {:status=>"changed", :tag=>"tag156", :node=>"vpfw-a"},
]

a2 = [
  {:status=>"success", :sender=>"ayfw-a"},
  {:status=>"success", :sender=>"vpfw-a"},
]

sender_status = Hash[ a2.map { |item| [ item[:sender], item[:status] ] } ]

a1.each do |item|
  item[:another_status] = sender_status[item[:node]] || 'skipped'
end

a1.each { |item| p item }

<强>输出

{:status=>"failed", :tag=>"tag156", :node=>"isfw-a", :another_status=>"skipped"}
{:status=>"unchanged", :tag=>"tag156", :node=>"ayfw-a", :another_status=>"success"}
{:status=>"changed", :tag=>"tag156", :node=>"vpfw-a", :another_status=>"success"}

答案 1 :(得分:-1)

a1 = [{:status=>"failed", :tag=>"tag156", :node=>"isfw-a"},
{:status=>"unchanged", :tag=>"tag156", :node=>"ayfw-a"},
{:status=>"changed", :tag=>"tag156", :node=>"vpfw-a"}]

a2 = [{:status=>"success", :sender=>"ayfw-a"},
{:status=>"success", :sender=>"vpfw-a"}]

a1.map do |h1| h1.merge(another_status:
  a2.find{|h2| h2[:sender] == h1[:node]}.to_h[:status] || "skipped"
) end

输出

[
  {
    :status         => "failed",
    :tag            => "tag156",
    :node           => "isfw-a",
    :another_status => "skipped"
  },
  {
    :status         => "unchanged",
    :tag            => "tag156",
    :node           => "ayfw-a",
    :another_status => "success"
  },
  {
    :status         => "changed",
    :tag            => "tag156",
    :node           => "vpfw-a",
    :another_status => "success"
  }
]

答案 2 :(得分:-1)

ar1 = [ {:status=>"failed", :tag=>"tag156", :node=>"isfw-a"},
        {:status=>"unchanged", :tag=>"tag156", :node=>"ayfw-a"},
        {:status=>"changed", :tag=>"tag156", :node=>"vpfw-a"}]

ar2 = [{:status=>"success", :sender=>"ayfw-a"},
       {:status=>"success", :sender=>"vpfw-a"}]

new_hsh = ar1.map do |a1| 
  bol = ar2.select{|a2| a2[:sender] == a1[:node] }
  bol ? a1[:another_status]=bol[:status] : a1[:another_status]= 'skipped'
  a1
end

new_ary
# => [{:status=>"failed",
#      :tag=>"tag156",
#      :node=>"isfw-a",
#      :another_status=>"skipped"},
#     {:status=>"unchanged",
#      :tag=>"tag156",
#      :node=>"ayfw-a",
#      :another_status=>"success"},
#     {:status=>"changed",
#      :tag=>"tag156",
#      :node=>"vpfw-a",
#      :another_status=>"success"}]
相关问题