复杂的红宝石阵列的哈希结合起来

时间:2015-03-20 17:50:31

标签: ruby arrays hash

我希望更改我的哈希数组,将所有具有相同键和不同值的哈希值组合到相同的哈希值中,这就是我的意思:

    [
    [0] {
        "Property 1" => {
            "Portal" => #<Client:0x70aa253d> {
                     :team_id => 1,
                     :cap_id => 25,
                     :user_id => 145,
                        :active => true,
                    :created_at => Wed, 18 Mar 2015 11:52:11 EDT -04:00,
                    :updated_at => Wed, 18 Mar 2015 11:52:11 EDT -04:00
            }
        }
    },
    [1] {
        "Property 2" => {
            "Client Solutions" => #<Client:0x70aa221c> {
                     :team_id => 2,
                     :cap_id => 25,
                   :user_id => 145,
                        :active => true,
                    :created_at => Wed, 18 Mar 2015 11:52:27 EDT -04:00,
                    :updated_at => Fri, 20 Mar 2015 12:32:52 EDT -04:00
            }
        }
    },
    [2] {
        "Property 1" => {
            "Other" => #<Client:0x680f8067> {
                     :team_id => 2,
                     :cap_id => 25,
                   :user_id => 145,
                        :active => true,
                    :created_at => Fri, 20 Mar 2015 12:52:23 EDT -04:00,
                    :updated_at => Fri, 20 Mar 2015 12:52:23 EDT -04:00
            }
        }
    }
]

因此,在合并之后,哈希数组应该如下所示:

[
        [0] {
            "Property 1" => {
                "Portal" => #<Client:0x70aa253d> {
                         :team_id => 1,
                         :cap_id => 25,
                         :user_id => 145,
                            :active => true,
                        :created_at => Wed, 18 Mar 2015 11:52:11 EDT -04:00,
                        :updated_at => Wed, 18 Mar 2015 11:52:11 EDT -04:00
                },
                "Other" => #<Client:0x680f8067> {
                         :team_id => 2,
                         :cap_id => 25,
                       :user_id => 145,
                            :active => true,
                        :created_at => Fri, 20 Mar 2015 12:52:23 EDT -04:00,
                        :updated_at => Fri, 20 Mar 2015 12:52:23 EDT -04:00
                }
            }
        },
        [1] {
            "Property 2" => {
                "Client Solutions" => #<Client:0x70aa221c> {
                         :team_id => 2,
                         :cap_id => 25,
                       :user_id => 145,
                            :active => true,
                        :created_at => Wed, 18 Mar 2015 11:52:27 EDT -04:00,
                        :updated_at => Fri, 20 Mar 2015 12:32:52 EDT -04:00
                }
            }
        }
    ]

我尝试了几件事,看起来像mergereverse_merge忽略了重复的值。假设result是包含哈希数组的变量,请尝试:

result.inject(:merge)
result.reduce(&:merge)
result.reduce({}, :reverse_merge)

根据我使用reverse_mergemerge的合并,我只能得到property 1中应该包含的1个值。

我在Portal中获得了OtherProperty,无法像他们所描述的那样获得他们,我做错了什么?

1 个答案:

答案 0 :(得分:1)

您遇到的问题是merge每个密钥会占用一个值。你想要一些不同的东西。

如果每个条目都不会有多个记录:

result.group_by {|r| r.keys.first }.each {|k, v| v.replace(v.flat_map(&:values)) }

一般的想法是我们通过每个条目找到的第一个键(在这种情况下是属性ID,因为它是唯一的键)对初始数组中的条目进行分组。这给了我们一个{property_id => [{property_id => record}, ...]}的哈希值,我们通过用每个记录中的值列表替换值来转换为正确的形式。