复杂的红宝石数组散列组合在一起 [英] Complex ruby array of hashes combine
问题描述
<$ p $我想要改变我的哈希数组,将具有相同键和不同值的所有哈希组合到同一个哈希中,这就是我的意思: p>
[
[0] {
Property 1=> {
Portal=> #<客户: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=> {
客户端解决方案=> #<客户: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 =>星期五,2015年3月20日12:32:52 EDT -04:00
}
}
},
[2] {
属性1=> {
其他=> #<客户:0x680f8067> {
:team_id => 2,
:cap_id => 25,
:user_id => 145,
:active => true,
:created_at =>星期五,2015年3月20日12:52:23 EDT -04:00,
:updated_at =>星期五,2015年3月20日12:52:23 EDT -04:00
}
}
}
]
$ p 所以在合并之后,哈希数组应该看起来像这样:
[
[0] {
Property 1=> {
Portal=> #<客户: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 =>星期三,18三月2015 11:52:11 EDT -04:00
},
其他=> #<客户:0x680f8067> {
:team_id => 2,
:cap_id => 25,
:user_id => 145,
:active => true,
:created_at =>星期五,2015年3月20日12:52:23 EDT -04:00,
:updated_at =>星期五,2015年3月20日12:52:23 EDT -04:00
}
}
},
[1] {
属性2=> {
客户端解决方案=> #<客户: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 =>星期五,2015年3月20日12:32:52 EDT -04:00
}
}
}
]
我尝试了几件事情,看起来像 merge
和 reverse_merge
忽略重复值。假设 result 是包含散列数组的变量,试过这个:
result.inject(:merge)
result.reduce(&:merge)
result.reduce({},:reverse_merge)
取决于使用哪一个合并 reverse_merge
或 merge
我只得到应该在属性1
中的值中的一个。
我得到
或属性
中的门户网站其他
,无法像描述的那样获得它们两个,我做错了什么?
您遇到的问题是 merge
将会为每个给定的键取一个值。
如果每个条目都不会有多条记录:
result.group_by {| r | r.keys.first} .each {| k,v | v.replace(v.flat_map(&):values))}
总的想法是,我们按照我们在每个条目中找到的第一个键(在本例中为属性ID,因为它是唯一键)对初始数组中的条目进行分组。这给了我们一个 {property_id => [{property_id =>记录},...]}
,我们通过用每个记录的值列表替换值来转换为适当的形式。
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.
这篇关于复杂的红宝石数组散列组合在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!