我想两个散列合并成一个阵列中的这样一种方式,可以让他们在这样的表合并两个散列,并将它们组合我只能将数组排列为这种格式: [{“动机”=> 4,“领导力”=> 3,“创新”=> 1},{“领导力”=> 2,“动机” => 3, “创新”=> 1}]通过密钥
在我的模型我有这样的代码:
def rates_table_index
total = []
...
...
slf_cc = ...
self_cc = {}
slf_cc.each do |cc|
self_cc[cc.title] = cc.rate
end
total << self_cc
other_cc = {}
other_ccs = ...
other_ccs.each do |cc|
other_cc[cc.title] = cc.rate
end
total << other_cc
result = total
end
标题和费率是来自用户的输入,所以我不知道如何按键组合值。
在我看来,我有这样的:
<h2>Table</h2>
<table class="table">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
</thead>
<tbody>
<% @model.rates_table_index.each do |title, values| %>
<tr>
<td><%= title %></td>
<td><%= values[0] %></td>
<td><%= values[1] %></td>
</tr>
<% end %>
</tbody>
</table>
谁能帮助我吗?
都将条目总是按键相同的长度? –
是,两个条目具有相同的密钥长度,只有值的数量可以因每个密钥而异。 –
对不起,我不明白你的意思。 “每个键的值的数量可能会有所不同。” ? –