这是我正在使用的数组:
[["5103", "2593", "1289796841"], ["1541", "99", "65990390"], ["934", "99", "73230517"], ["3057", "99", "62377700"], ["1410", "99", "81193882"], ["1232", "99", "50959566"], ["341", "99", "31225295"], ["2303", "99", "46585590"], ["91", "99", "200000000"], ["2259", "99", "32250727"], ["692", "99", "40608716"], ["4397", "99", "23545788"], ["2372", "99", "30486082"], ["408", "99", "33064494"], ["136", "99", "54937860"], ["2412", "99", "23192056"], ["858", "99", "30378477"], ["1088", "99", "21174680"], ["174", "99", "76296917"], ["477", "99", "50883493"], ["2578", "99", "24367856"], ["603", "99", "34401457"], ["1556", "99", "24433483"], ["1106", "99", "22180782"], ["1365", "99", "23373048"], ["4889", "120", "121954995"], ["10967", "97", "10702990"], ["-1", "-1"], ["-1", "-1"], ["9462", "3985121"], ["-1", "-1"], ["-1", "-1"], ["25914", "1608"], ["17184", "1538"], ["19936", "1519"], ["33498", "1651"], ["-1", "-1"], ["2589", "401"], ["14280", "1262"], ["-1", "-1"], ["-1", "-1"], ["-1", "-1"], ["32068", "378140"], ["-1", "-1"], ["-1", "-1"], ["-1", "-1"]]
这是一个大型数组,里面有许多较小的数组。每个小数组的数字代表等级,然后是等级,然后是经验。
这是另一个包含每个数字的标签的数组:
@skills = %w(overall, attack, defence, strength, constitution, ranged, prayer, magic, cooking, woodcutting, fletching, fishing, firemaking, crafting, smithing, mining, herblore, agility, thieving, slayer, farming, runecrafting, hunter, construction, summoning, dungeoneering, divination)
以下是我要创建的表格:
<table>
<tr>
<th>Skill</th>
<th>Level</th>
<th>Experience</th>
<th>Rank</th>
</tr>
<% @skills.each do |skill| %>
<tr>
<td><%= skill.chomp(',').capitalize %></td>
</tr>
<% end %>
</table>
我已经将每个技能的名称放在表格的各一行上。我现在需要做的是将每项技能的等级,等级和经验放在表格中技能名称旁边。
有没有办法迭代每个子数组的位置x中的每个元素?那么每个小数组中的每个第一个数字都在一列中?
修改
这就是我现在所拥有的:
<table>
<tr>
<th>Skill</th>
<th>Level</th>
<th>Experience</th>
<th>Rank</th>
</tr>
<% @skills.each do |skill| %>
<tr>
<td><%= skill.chomp(',').capitalize %></td>
</tr>
<% end %>
<% @skills.each_with_index do |skill, i| %>
<tr>
<td><%= @stats[i][0] %></td>
<td><%= @stats[i][1] %></td>
<td><%= @stats[i][2] %></td>
</tr>
<% end %>
</table>
以下是页面上输出的内容:
整个底部桌子需要支撑在顶部的右侧。
答案 0 :(得分:1)
使用each_with_index
方法:
<table>
<tr>
<th>Skill</th>
<th>Level</th>
<th>Experience</th>
<th>Rank</th>
</tr>
<% @skills.each_with_index do |skill, i| %>
<tr>
<td><%= skill.chomp(',').capitalize %></td>
<td><%= @stats[i][0] %></td>
<td><%= @stats[i][1] %></td>
<td><%= @stats[i][2] %></td>
</tr>
<% end %>
</table>
可替换地:
<% @skills.each_with_index do |skill, i| %>
<tr>
<td><%= skill.chomp(',').capitalize %></td>
<%= @stats[i].map{ |s| "<td>#{s}</td>" }.join %>
</tr>
<% end %>
BTW:您可以删除%w()
字面上的逗号并删除chomp(',')
部分。