Say you have an ordered array like this, generated from a database of addresses:
[
{ city: Sacramento, state: CA },
{ city: San Francisco, state: CA },
{ city: Seattle, state: WA }
]
And you want to generate HTML with it like this:
<p>CA</p>
<ul>
<li>Sacramento</li>
<li>San Francisco</li>
</ul>
<p>WA</p>
<ul>
<li>Seattle</li>
</ul>
So you're grouping by state. One approach to doing this would be to remember the last row on each iteration of the loop and to display the state and bookending UL tags only if the current row's state is the same as the last rows state. That seems kind of nasty and non Ruby-y.
Anyone have any advice on an elegant Ruby/Rails approach to this?
Enumerable#group_by
?
array = [
{city: 'Sacramento', state: 'CA'},
{city: 'San Francisco', state: 'CA'},
{city: 'Seattle', state: 'WA'}
]
array.group_by{|elem| elem[:state]}
# => {"CA"=>[{:city=>"Sacramento", :state=>"CA"}, {:city=>"San Francisco", :state=>"CA"}], "WA"=>[{:city=>"Seattle", :state=>"WA"}]}
Enumerable has group_by
cities = [
{ city: "Sacramento", state: "CA" },
{ city: "San Francisco", state: "CA" },
{ city: "Seattle", state: "WA" }]
cities.group_by {|c| c[:state]}
=> {"CA"=>[{:city=>"Sacramento", :state=>"CA"},
{:city=>"San Francisco", :state=>"CA"}],
"WA"=>[{:city=>"Seattle", :state=>"WA"}]}
I'm kind of rusty on ERB but I think it would be something like this
<% @cities_by_state.each do |state, cities| %>
<p><%= state %></p>
<ul>
<% cities.each do |city| %>
<li><%= city[:city] %></li>
<% end %>
</ul>
<% end %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With