user user - 9 months ago 54
Ruby Question

Merge two array of hashes

I have two arrays of hashes and want to do one from it

first =
[{:frontman=>"aaa", :category=>"bbb", :subcategory=>nil, :detail=>nil},other hashes]

second =
[{:__content__=>"aaa", :id=>"9096290", :frontman=>"aaa"},other hashes]

I want to have

one large array of hashes

[{:__content__=>"aaa", :id=>"9096290", :frontman=>"aaa", :category=>"bbb", :subcategory=>nil, :detail=>nil},other hashes]

i have tried it

(first+second).group_by{|h| h[:frontman]}.map{|k,v| v.reduce(:merge)}

but it doesn't work for me


You can use this way: { |f, s| f.merge(s) }
#=> [{:frontman=>"aaa", :category=>"bbb", :subcategory=>nil, ...}]