Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby transformation to merge an Array of Hashes into another Array of Hash?

Tags:

ruby

I have

 [
   {:date => "2012-05", :post => 1}, 
   {:date => "2012-12", :post => 1}, 
   {:date => "2013-02", :post => 1}, 
   {:date => "2012-05", :online => 1}
 ]

And I want to get:

[
  {:date => "2012-05", :post => 1, :online => 1}, 
  {:date => "2012-12", :post => 1 }, 
  {:date => "2013-02", :post => 1 }
]

Anyone sees how to apply Ruby hash/array methods to achieve this?

like image 937
poseid Avatar asked Dec 12 '25 13:12

poseid


1 Answers

q.group_by { |x| x[:date] }.values.map { |e| e.reduce :merge }
like image 91
DigitalRoss Avatar answered Dec 15 '25 12:12

DigitalRoss



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!