我正在尝试显示2维数组。使用下面的代码。但是对于每个数组的第一个下标,我必须编写一个循环。我可以使用外循环而不是多循环吗?
<% tempTickets = subject['permailaddr-addrline1'] %>
<% cnt = tempTickets.length %>
<% tempTickets.each_with_index do |ticket, index| %>
<div>
<tr>
<td><li class="entity_info"><div><span class="label"><%= subject['memhead-mem-recno'][index] %></span></div></li></td>
<td><li class="entity_info"><div><span class="label"><%= subject['permailaddr-addresstype'][index] %></span></div></li></td>
<td><li class="entity_info"><div><span class="label"><%= subject['permailaddr-addrline1'][index] %></span></div></li></td>
<td><li class="entity_info"><div><span class="label"><%= subject['permailaddr-state'][index] %></span></div></li></td>
<td><li class="entity_info"><div><span class="label"><%= subject['permailaddr-suburb'][index] %></span></div></li></td>
<td><li class="entity_info"><div><span class="label"><%= subject['permailaddr-postcode'][index] %></span></div></li></td>
<td><li class="entity_info"><div><span class="label"><%= subject['permailaddr-countrycode'][index] %></span></div></li></td>
</tr>
</div>
<% end %>
<% tempTickets = subject['perresiaddr-addrline1'] %>
<% cnt = tempTickets.length %>
<% tempTickets.each_with_index do |ticket, index| %>
<div>
<tr>
<td><li class="entity_info"><div><span class="label"><%= subject['memhead-mem-recno'][index] %></span></div></li></td>
<td><li class="entity_info"><div><span class="label"><%= subject['perresiaddr-addresstype'][index] %></span></div></li></td>
<td><li class="entity_info"><div><span class="label"><%= subject['perresiaddr-addrline1'][index] %></span></div></li></td>
<td><li class="entity_info"><div><span class="label"><%= subject['perresiaddr-state'][index] %></span></div></li></td>
<td><li class="entity_info"><div><span class="label"><%= subject['perresiaddr-suburb'][index] %></span></div></li></td>
<td><li class="entity_info"><div><span class="label"><%= subject['perresiaddr-postcode'][index] %></span></div></li></td>
<td><li class="entity_info"><div><span class="label"><%= subject['perresiaddr-countrycode'][index] %></span></div></li></td>
</tr>
</div>
<% end %>
答案 0 :(得分:1)
ticketLists = [ subject['permailaddr-addrline1'], subject['perresiaddr-addrline1'] ]
ticketLists.each do |tempTickets|:
然后包含要为每个tempTickets数组执行的代码。
答案 1 :(得分:0)
尝试,
2.times do |i|
...
<%= subject['#{ i==0 ? "permailaddr" : "perresiaddr"}-addresstype'][index] %>
...
end
答案 2 :(得分:0)
这样的事情应该有效。
<% ['permailaddr', 'perresiaddr'].each do |prefix| %>
<% tempTickets.each_with_index do |ticket, index| %>
<div>
<tr>
<td><li class="entity_info"><div><span class="label"><%= subject['memhead-mem-recno'][index] %></span></div></li></td>
<% ['addresstype', 'addrline1', 'state', 'suburb', 'postcode', 'countrycode'].each do |field_name| %>
<td><li class="entity_info"><div><span class="label"><%= subject["#{prefix}-#{field_name}"][index] %></span></div></li></td>
<% end %>
</tr>
</div>
<% end %>
<% end %>
注意我还将字段名称 - ['addresstype', 'addrline1', 'state', 'suburb', 'postcode', 'countrycode']
- 拉出到一个数组中并循环遍历它们,以保存一些重复的html - 但如果你不想要它,则不需要它。