简体   繁体   English

复杂的红宝石哈希组合

[英]Complex ruby array of hashes combine

I'm looking to change my array of hashes, to combine all hashes with same keys and different values into same hash, here is what I mean: 我想更改我的哈希数组,将具有相同键和不同值的所有哈希组合到相同的哈希中,这就是我的意思:

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

So after the merge the array of hashes should look like this : 因此,合并后的哈希数组应如下所示:

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

I tried couple of things, looks like merge and reverse_merge are ignoring the duplicate values. 我尝试了几件事,看起来mergereverse_merge忽略了重复的值。 Assuming result is the variable holding the array of hashes, tried this : 假设result是保存哈希数组的变量,请尝试以下操作:

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

Depending on which merge do I use reverse_merge or merge I get only 1 of the values that should be in property 1 . 根据我使用的是reverse_merge还是merge我只能得到property 1

I get either Portal or Other in the Property , can't get both of them like described, what am I doing wrong? 我在该Property获得了PortalOther ,无法像所描述的那样获得两者,我做错了什么?

The issue you're having is that merge is going to take one value per key given. 您遇到的问题是, merge将为给定的每个键取一个值。 You want something a little different here. 您想在这里有所不同。

If each entry won't ever have more than one record: 如果每个条目都不会有一个以上的记录:

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

The general idea is that we group the entries in the initial array by the first key we find per entry (which in this case is the property ID, since it's the only key). 一般的想法是,我们将按每个条目找到的第一个键将初始数组中的条目分组(在本例中是属性ID,因为它是唯一的键)。 That gives us a Hash of {property_id => [{property_id => record}, ...]} , which we convert into the proper form by replacing the values with a list of the values from each record. 这为我们提供了{property_id => [{property_id => record}, ...]}的哈希,通过将值替换为每个记录的值列表,我们将其转换为适当的格式。

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

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