【发布时间】:2014-05-19 15:04:01
【问题描述】:
我有以下两个数组:
Classification.product.map{|p|[p.description, p.id]}
[["Antiques", 1], ["Art", 2], ["Auto", 3], ["Books", 4], ["Clothing & Accessories", 5], ["Collectibles/Trading Cards", 6], ["Electronics & Computers", 7], ["Event Decoration", 8], ["Furniture", 9], ["Home & Garden", 10], ["Instruments", 11], ["Kids & Baby", 12], ["Movies & Music", 13], ["Property Rentals", 14], ["Shoes/Sneakers", 15], ["Sports & Outdoors", 16], ["Tickets", 17], ["Toys & Games", 18], ["Video Games", 19], ["Other", 20]]
和
Classification.service.map{|p|[p.description, p.id]}
[["Beauty", 21], ["Child Care", 22], ["Cleaning", 23], ["Computer/Technology", 24], ["Culinary", 25], ["Custom Goods", 26], ["Decorating/Interior Design", 27], ["Fashion", 28], ["Fitness", 29], ["Health/Wellbeing", 30], ["Home Improvement", 31], ["Landscaping/Yard", 32], ["Odd Jobs/Handymen", 33], ["Organizing", 34], ["Event Planning", 35], ["Pet Care", 36], ["Photography/Video", 37], ["Professional Services", 38], ["Tutoring/Instruction", 39], ["Other", 40]]
我想要做的是创建一个散列,其中每个键是第一个数组中的一个元素,每个值是第二个数组中的一个元素。
所以,理想情况下它应该是这样的:
{["Antiques", 1]=>["Beauty", 21], ["Art", 2]=>["Child Care", 22], etc.}
恰好有相同数量的产品和服务分类;但是,如果它们不相等,我希望代码仍然可以工作。因此,如果其中一个的分类少于另一个,它应该插入一个空白键/值。
我该怎么做/可以做到吗?
【问题讨论】: