1

假设我有这些数据:

[{"sku"=>"reprint"}, [], nil, {"quantity"=>"1"}, [], nil, {"shippingOptions"=>"PQRS"},
[{"option"=>"PQRS"}], nil, {"abc"=>"abcccc"}, 
[], nil,{"sku"=>"poster"}, 
[], nil, {"quantity"=>"2"}, [], nil, {"shippingOptions"=>"MNLIJK"},
[{"option"=>"MNL"}, {"option"=>"IJK"}], nil, {"sku"=>"cards"}, 
[], nil, {"quantity"=>"5"},
[], nil, {"shippingOptions"=>"DEFABC"}, [{"option"=>"DEF"}, {"option"=>"ABC"}], nil] 

我想使用 Ruby on Rails 迭代这些数据并以适当的格式将其打印为 html 我是这项技术的新手。任何帮助表示赞赏!

4

1 回答 1

2

.flatten在这里可能很方便。如果您不关心结构,我会在迭代之前将其展平,那么您不必担心不同的深度。

1.9.3p194 :001 > root = [{"sku"=>"reprint"}, [], nil, {"quantity"=>"1"}, [], nil, {"shippingOptions"=>"PQRS"},
1.9.3p194 :002 >     [{"option"=>"PQRS"}], nil, {"abc"=>"abcccc"}, 
1.9.3p194 :003 >     [], nil,{"sku"=>"poster"}, 
1.9.3p194 :004 >     [], nil, {"quantity"=>"2"}, [], nil, {"shippingOptions"=>"MNLIJK"},
1.9.3p194 :005 >     [{"option"=>"MNL"}, {"option"=>"IJK"}], nil, {"sku"=>"cards"}, 
1.9.3p194 :006 >     [], nil, {"quantity"=>"5"},
1.9.3p194 :007 >     [], nil, {"shippingOptions"=>"DEFABC"}, [{"option"=>"DEF"}, {"option"=>"ABC"}], nil]
 => [{"sku"=>"reprint"}, [], nil, {"quantity"=>"1"}, [], nil, {"shippingOptions"=>"PQRS"}, [{"option"=>"PQRS"}], nil, {"abc"=>"abcccc"}, [], nil, {"sku"=>"poster"}, [], nil, {"quantity"=>"2"}, [], nil, {"shippingOptions"=>"MNLIJK"}, [{"option"=>"MNL"}, {"option"=>"IJK"}], nil, {"sku"=>"cards"}, [], nil, {"quantity"=>"5"}, [], nil, {"shippingOptions"=>"DEFABC"}, [{"option"=>"DEF"}, {"option"=>"ABC"}], nil] 
1.9.3p194 :008 > root.flatten!
 => [{"sku"=>"reprint"}, nil, {"quantity"=>"1"}, nil, {"shippingOptions"=>"PQRS"}, {"option"=>"PQRS"}, nil, {"abc"=>"abcccc"}, nil, {"sku"=>"poster"}, nil, {"quantity"=>"2"}, nil, {"shippingOptions"=>"MNLIJK"}, {"option"=>"MNL"}, {"option"=>"IJK"}, nil, {"sku"=>"cards"}, nil, {"quantity"=>"5"}, nil, {"shippingOptions"=>"DEFABC"}, {"option"=>"DEF"}, {"option"=>"ABC"}, nil]  
1.9.3p194 :011 > root.each do |r| 
1.9.3p194 :012 >     next if r.nil?
1.9.3p194 :013?>     puts r.to_s
1.9.3p194 :014?> end
{"sku"=>"reprint"}
{"quantity"=>"1"}
{"shippingOptions"=>"PQRS"}
{"option"=>"PQRS"}
{"abc"=>"abcccc"}
{"sku"=>"poster"}
{"quantity"=>"2"}
{"shippingOptions"=>"MNLIJK"}
{"option"=>"MNL"}
{"option"=>"IJK"}
{"sku"=>"cards"}
{"quantity"=>"5"}
{"shippingOptions"=>"DEFABC"}
{"option"=>"DEF"}
{"option"=>"ABC"}

回复:评论。

data = [{"sku"=>"reprint"}, [], nil, {"quantity"=>"1"}, [], nil, {"shippingOptions"=>"PQRS"}, [{"option"=>"PQRS"}], nil, {"abc"=>"abcccc"}, [], nil, {"sku"=>"poster"}, [], nil, {"quantity"=>"2"}, [], nil, {"shippingOptions"=>"MNLIJK"}, [{"option"=>"MNL"}, {"option"=>"IJK"}], nil, {"sku"=>"cards"}, [], nil, {"quantity"=>"5"}, [], nil, {"shippingOptions"=>"DEFABC"}, [{"option"=>"DEF"}, {"option"=>"ABC"}], nil]
hash_collection = data.flatten.map(&:to_a).flatten(1).reduce({}) { |h,(k,v)| (h[k] ||= []) << v; h}

产生:

=> {"sku"=>["reprint", "poster", "cards"], "quantity"=>["1", "2", "5"], "shippingOptions"=>["PQRS", "MNLIJK", "DEFABC"], "option"=>["PQRS", "MNL", "IJK", "DEF", "ABC"], "abc"=>["abcccc"]}

我实际上希望我的新数据是这样的:

{{"sku"=>"Reprint"},{"quantity"=>"1"},{"option"=>"PQRS"}}
{{"sku"=>"Poster"},{"quantity"=>"2"},{"option"=>"MNL"},{"option"=>"IJK"}}
{{"sku"=>"Cards"}, {"quantity"=>"5"},{"option"=>"DEF"},{"option"=>"ABC"}}
{{"sku"=>"Books"},{"quantity"=>"6"},{"option"=>"QIU"},{"option"=>"PSJ"},   option"=>"IQA"},{"suboption"=>"ikh"},{"subsuboption"=>"uuuuuuuuuuuuuuuuu"}}
于 2012-12-13T17:07:11.247 回答