简体   繁体   中英

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. 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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