Just use array.map { ... }.flatten
.
To get all counties, you'd use...
@counties = @states.map { |o| o[:states] }.flatten
If you wish to flatten by only one level (which flat_map
does in current versions of Ruby), you can pass a 1
to flatten
. This is unnecessary for your example, as you are building an array with at most two dimensions.