Complex ruby array of hashes combine

血红的双手。 提交于 2020-01-06 19:32:02

问题


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. Assuming result is the variable holding the array of hashes, tried this :

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.

I get either Portal or Other in the Property, can't get both of them like described, what am I doing wrong?


回答1:


The issue you're having is that merge is going to take one value per key given. 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). 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.



来源:https://stackoverflow.com/questions/29172821/complex-ruby-array-of-hashes-combine

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!