Reputation: 49
So I want to convert in ruby
[{:user_id => 4}, {:user_id => 22}, {:user_id=>51}, {:user_id=>52}]
to
[4, 22, 51, 52]
Is there way of convert this?
Upvotes: 2
Views: 403
Reputation: 30056
Very simple, let's use map to transform each item in something else
array.map { |item| item[:user_id] } => [4, 22, 51, 52]
Reputation: 66
As simple as possible: array.flat_map(&:values)
array.flat_map(&:values)