Ruby - 哈希到数组 - 访问密钥

时间:2014-10-15 08:38:40

标签: ruby

我在Ruby中使用一系列方法有点不舒服。在这里,我使用了这样的链条。

所以问题是,告诉我一个更好的方法来做同样的事情。

我有这一行,

p["All_Products"]["Product"].map{|h| h["Categories"]["Category"] rescue nil }.compact.flatten.uniq

p等于此,

{"All_Products"=>
  {"Product"=>
    [{"ProductCode"=>"GFT",
      "Categories"=>
       {"Category"=>
         [{"CategoryID"=>"1513", "CategoryName"=>"Beauty"},
          {"CategoryID"=>"1515", "CategoryName"=>"Specials"},
          {"CategoryID"=>"1553", "CategoryName"=>"Bath and Body"}]}},
     {"ProductCode"=>"GF-honey",
      "Categories"=>
       {"Category"=>
         [{"CategoryID"=>"1514", "CategoryName"=>"Gourmet Food"},
          {"CategoryID"=>"1515", "CategoryName"=>"Specials"},
          {"CategoryID"=>"1632", "CategoryName"=>"Specialty Items"}]}},
     {"ProductCode"=>"GF-malbec",
      "Categories"=>
       {"Category"=>
         [{"CategoryID"=>"1514", "CategoryName"=>"Gourmet Food"},
          {"CategoryID"=>"1632", "CategoryName"=>"Specialty Items"}]}},
     {"ProductCode"=>"GF-olives",
      "Categories"=>
       {"Category"=>
         [{"CategoryID"=>"1514", "CategoryName"=>"Gourmet Food"},
          {"CategoryID"=>"1632", "CategoryName"=>"Specialty Items"}]}},
     {"ProductCode"=>"HC-flatiron",
      "Categories"=>
       {"Category"=>
         [{"CategoryID"=>"1513", "CategoryName"=>"Beauty"},
          {"CategoryID"=>"1515", "CategoryName"=>"Specials"},
          {"CategoryID"=>"1554", "CategoryName"=>"Hair Care"}]}}
    ]
  } 
}

最后,我得到了这个,

[{"CategoryID"=>"1513", "CategoryName"=>"Beauty"},
 {"CategoryID"=>"1515", "CategoryName"=>"Specials"},
 {"CategoryID"=>"1553", "CategoryName"=>"Bath and Body"},
 {"CategoryID"=>"1514", "CategoryName"=>"Gourmet Food"},
 {"CategoryID"=>"1632", "CategoryName"=>"Specialty Items"},
 {"CategoryID"=>"1554", "CategoryName"=>"Hair Care"},
 {"CategoryID"=>"1673", "CategoryName"=>"Sweets"},
 {"CategoryID"=>"1477", "CategoryName"=>"Home Decor"},
 {"CategoryID"=>"1516", "CategoryName"=>"Furniture"},
 {"CategoryID"=>"1517", "CategoryName"=>"Home Accessories"},
 {"CategoryID"=>"1814", "CategoryName"=>"Apparel"},
 {"CategoryID"=>"1815", "CategoryName"=>"Women"},
 {"CategoryID"=>"1816", "CategoryName"=>"Men"}]

1 个答案:

答案 0 :(得分:1)

这会有帮助吗?:

products = {"All_Products"=>
  {"Product"=>
    [{"ProductCode"=>"GFT",
      "Categories"=>
       {"Category"=>
         [{"CategoryID"=>"1513", "CategoryName"=>"Beauty"},
          {"CategoryID"=>"1515", "CategoryName"=>"Specials"},
          {"CategoryID"=>"1553", "CategoryName"=>"Bath and Body"}]}},
     {"ProductCode"=>"GF-honey",
      "Categories"=>
       {"Category"=>
         [{"CategoryID"=>"1514", "CategoryName"=>"Gourmet Food"},
          {"CategoryID"=>"1515", "CategoryName"=>"Specials"},
          {"CategoryID"=>"1632", "CategoryName"=>"Specialty Items"}]}},
     {"ProductCode"=>"GF-malbec",
      "Categories"=>
       {"Category"=>
         [{"CategoryID"=>"1514", "CategoryName"=>"Gourmet Food"},
          {"CategoryID"=>"1632", "CategoryName"=>"Specialty Items"}]}},
     {"ProductCode"=>"GF-olives",
      "Categories"=>
       {"Category"=>
         [{"CategoryID"=>"1514", "CategoryName"=>"Gourmet Food"},
          {"CategoryID"=>"1632", "CategoryName"=>"Specialty Items"}]}},
     {"ProductCode"=>"HC-flatiron",
      "Categories"=>
       {"Category"=>
         [{"CategoryID"=>"1513", "CategoryName"=>"Beauty"},
          {"CategoryID"=>"1515", "CategoryName"=>"Specials"},
          {"CategoryID"=>"1554", "CategoryName"=>"Hair Care"}]}}
    ]
  } 
}

然后你可以:

puts products['All_Products']['Product'].
     collect{ |product| product['Categories'] && product['Categories']['Category'] }.
     flatten.uniq

结果:

[{"CategoryID"=>"1513", "CategoryName"=>"Beauty"},
 {"CategoryID"=>"1515", "CategoryName"=>"Specials"},
 {"CategoryID"=>"1553", "CategoryName"=>"Bath and Body"},
 {"CategoryID"=>"1514", "CategoryName"=>"Gourmet Food"}, 
 {"CategoryID"=>"1632", "CategoryName"=>"Specialty Items"}, 
 {"CategoryID"=>"1554", "CategoryName"=>"Hair Care"}]

另外,请尽量不要创建变量名称,例如pputsdo等。它们是保留的Ruby方法。