Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert array of hashes to array

Tags:

arrays

ruby

hash

I have an array of hashes from which I need the values of the hashes in a new array. The array of hashes look likes this, with a couple thousand of them.

array = [{:code=>"404"}, {:code=>"302"}, {:code=>"200"}]

I have tried to look this up but have only found out how to convert from a hash.

How would I go about doing this?

like image 823
tbrent Avatar asked Feb 28 '26 08:02

tbrent


2 Answers

[{:code=>"404"}, {:code=>"302"}, {:code=>"200"}].flat_map(&:values)
#⇒ ["404", "302", "200"]
like image 179
Aleksei Matiushkin Avatar answered Mar 02 '26 21:03

Aleksei Matiushkin


a=[{:code=>"404"}, {:code=>"302"}, {:code=>"200"}] 
puts a.map{|x|x.values}.flatten.inspect

output

["404", "302", "200"]
like image 36
Gopal Avatar answered Mar 02 '26 21:03

Gopal



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!