现在我的rails应用程序中有这样的数据:
[[{:car=>"", :article=>"", :group=>"", :price=>""},
{:car=>"Volkswagen", :article=>"7H0127401A", :group=>"FILTER", :price=>"0,564"},
{:car=>"Volkswagen", :article=>"7H0127401B", :group=>"FILTER", :price=>"0,546"},
{:car=>"Volkswagen", :article=>"7H0127401D", :group=>"FILTER", :price=>"0,42"}],
[{:car=>"", :article=>"", :group=>"", :price=>""},
{:car=>"Volkswagen", :article=>"7H0127401A", :group=>"FILTER", :price=>"0,564"}],
[{:car=>"", :article=>"", :group=>"", :price=>""},
{:car=>"Volkswagen", :article=>"7H0127401B", :group=>"FILTER", :price=>"0,546"}]]
并查看:
=@oem_art.each do |oem|
-oem.each do |oo|
%tr{:class => cycle("zebra-stripe zebra-grey zebra1", "zebra-stripe zebra-grey zebra2")}
%td
= oo[:car]
%td
%h4
= oo[:article]
%td
= oo[:group]
%td
= oo[:price]
但我怎么能以更美丽的方式做到这一点?另外如何使用空键跳过哈希? 我怎么能通过标准的一个循环来做,例如:
=@oem_art.each do |oem|
= oem.car
%td
=oem.price
怎么样呢?但这里没有用完......
UPD
def original_art
@article = Article.find_by_ART_ID(params[:id])
@constr_num = ArtLookup.get_construction_number(@article.ART_ID)
oem_art = []
@constr_num.each do |o|
as_oem = get_from_as_oem(o.ARL_SEARCH_NUMBER)
if as_oem.present?
oem_art << as_oem
end
end
@oem_art = oem_art.to_a.uniq
@article = Article.first
@oem_art
end
答案 0 :(得分:0)
美是个人品味的问题,flatten
有助于避免嵌套循环。
- @oem_art.flatten.reject { |h| h[:car].empty? }.each do |oem|
= oem[:car]
# And so on ...
答案 1 :(得分:0)
这是我在问题评论中提出的观点。如果您有oem_art
:
[[{:car=>"", :article=>"", :group=>"", :price=>""},
{:car=>"Volkswagen", :article=>"7H0127401A", :group=>"FILTER", :price=>"0,564"},
{:car=>"Volkswagen", :article=>"7H0127401B", :group=>"FILTER", :price=>"0,546"},
{:car=>"Volkswagen", :article=>"7H0127401D", :group=>"FILTER", :price=>"0,42"}],
[{:car=>"", :article=>"", :group=>"", :price=>""},
{:car=>"Volkswagen", :article=>"7H0127401A", :group=>"FILTER", :price=>"0,564"}],
[{:car=>"", :article=>"", :group=>"", :price=>""},
{:car=>"Volkswagen", :article=>"7H0127401B", :group=>"FILTER", :price=>"0,546"}]]
即。要素是:
1. [{:car=>"", :article=>"", :group=>"", :price=>""},
{:car=>"Volkswagen", :article=>"7H0127401A", :group=>"FILTER", :price=>"0,564"},
{:car=>"Volkswagen", :article=>"7H0127401B", :group=>"FILTER", :price=>"0,546"},
{:car=>"Volkswagen", :article=>"7H0127401D", :group=>"FILTER", :price=>"0,42"}]
2. [{:car=>"", :article=>"", :group=>"", :price=>""},
{:car=>"Volkswagen", :article=>"7H0127401A", :group=>"FILTER", :price=>"0,564"}]
3. [{:car=>"", :article=>"", :group=>"", :price=>""},
{:car=>"Volkswagen", :article=>"7H0127401B", :group=>"FILTER", :price=>"0,546"}]
如果你致电oem_art.uniq
,这将删除重复的元素,但3个元素都是不同的,所以不会删除任何内容。它实际上不会从 元素中删除7H0127401A和7H0127401B的重复项。