我有以下哈希。
@facet_counts = {"facet_queries"=>{},
"facet_fields"=>
{"product_collection_value"=>["traditional and imitation", 304, "chunky", 34, "modern", 15, "coloured gems", 12, "traditional", 0, "traditional & imitation", 0],
"product_material_value"=>["alloy", 161, "metal alloy", 132, "metal", 60, "925 sterling silver", 8, "lac", 3, "beads", 2, "beaded", 0, "brass", 0, "copper", 0, "crystal", 0, "fabric", 0, "feather", 0, "glass", 0, "jute", 0, "leather", 0, "pashmina", 0, "plastic", 0, "polymer beads", 0, "pu leather", 0, "rexin", 0, "rubber", 0, "satin", 0, "shell", 0, "silk", 0, "silk brocade", 0, "silver", 0, "silver alloy", 0, "stainless steel", 0, "sterling silver", 0, "stone", 0, "velvet", 0, "viscose", 0, "wood", 0, "wooden", 0, "wool", 0],
"product_type_value"=>["jhumkis", 364, "danglers", 53, "drops", 7, "hoops", 6, "victorian", 2, "armlets", 0, "bands", 0, "bangles", 0, "beaded", 0, "beads", 0, "bib", 0, "chains", 0, "charms", 0, "choker", 0, "clip on", 0, "cluster", 0, "cluster pendant necklaces", 0, "clusters", 0, "cocktail", 0, "contemporary", 0, "cuff", 0, "cz", 0, "diamond look", 0, "double chain", 0, "double fold", 0, "double strand", 0, "earrings", 0, "fashion", 0, "gemstone", 0, "hasli", 0, "hath phool", 0, "kada", 0, "kamarband", 0, "kundan", 0, "link", 0, "links", 0, "maang tika set", 0, "mangalsutras", 0, "modern", 0, "oxidised", 0, "oxidized", 0, "pair", 0, "pearl", 0, "pendant", 0, "pendant necklaces", 0, "potli", 0, "pouch", 0, "rani haar", 0, "rings", 0, "saree pins", 0, "set", 0, "single chain", 0, "single fold", 0, "single stone", 0, "single strand", 0, "singles", 0, "sling bag", 0, "strings", 0, "studded", 0, "studs", 0, "thewa", 0, "tote bag", 0, "traditional", 0, "with chain", 0, "with gemstone", 0, "without chain", 0, "without gemstone", 0],
"product_plating_value"=>["yellow gold plating", 135, "gold plating", 98, "silver", 39, "black silver", 15, "rhodium", 2, "white rhodium", 1, "14k yellow gold", 0, "18k yellow gold", 0, "alloy", 0, "black gold", 0, "black rhodium", 0, "brass", 0, "cubic zirconia", 0, "oxidised", 0, "pearl", 0, "rose gold", 0, "rose gold plating", 0, "silver plating", 0, "sterling silver", 0, "yellow gold", 0, "yellow rhodium", 0],
"product_gemstones_value"=>["cubic zirconia", 132, "pearl", 45, "semi-precious", 19, "crystal", 3, "precious", 3, "amethyst", 2, "citrine", 2, "garnet", 2, "peridot", 2, "green onyx", 1, "imitation kundan", 1, "iolite", 1, "aquamarine", 0, "black onyx", 0, "blue topaz", 0, "carnelian", 0, "chalcedony", 0, "coral", 0, "diamond", 0, "emerald", 0, "gem stones", 0, "green stone", 0, "howlite", 0, "hydro", 0, "jade", 0, "jasper", 0, "kundan", 0, "labradorite", 0, "lapis", 0, "lemon quartz", 0, "lemon stone", 0, "malachite", 0, "marcasite", 0, "moonstone", 0, "onyx", 0, "opal", 0, "pink amethyst", 0, "prehnite", 0, "quartz", 0, "rainbow", 0, "red onyx", 0, "red stone", 0, "red tiger eye", 0, "rhodolite", 0, "rose quartz", 0, "ruby", 0, "sapphire", 0, "smoky quartz", 0, "spinel", 0, "tanzanite", 0, "tiger eye", 0, "topaz", 0, "tourmaline", 0, "turquoise", 0, "white rainbow", 0, "white rainbow stone", 0],
"product_occasion_value"=>["special occasions or gifts", 358, "wedding or festive wear", 245, "everyday wear", 119, "work wear", 4, "religious", 0]
},
"facet_dates"=>{},
"facet_ranges"=>{}}
我想要的内容哈希值对应页面上的搜索结果。针对每个字段名称的数组表示具有该值的值和结果的数量。但由于这是一个数组,我无法轻易访问计数。
我目前正在做的是:
facet_fields=["product_collection_value","product_material_value","product_type_value","product_plating_value","product_gemstones_value","product_occasion_value"]
@count_hash = Hash.new
facet_fields.each {|field|
print @facet_counts["facet_fields"][field]
@facet_counts["facet_fields"][field].each_with_index{ |v,i|
if i%2 == 1
next
else
@count_hash[@facet_counts["facet_fields"][field][i]] = @facet_counts["facet_fields"][field][i+1]
end
}
print "\n\n"
}
问题:这会创建一个新的哈希。但问题是,如果同一个标签有多个条目。例如。 Modern
位于product_collection_value
和product_type_value
,因此值会被覆盖。有没有办法可以转换原始哈希值,以便可以轻松访问计数?
答案 0 :(得分:1)
我相信你想要做到这一点:
@count_hash = @facet_counts['facet_fields'].map do |k, v|
Hash[*v]
end.inject({}) do |s, i|
s.merge(i) { |_, old, new| old + new }
end
第一部分将每个facet字段映射到键值对的散列,例如 - 第一部分看起来像这样:
{"traditional and imitation"=>304,
"chunky"=>34, "modern"=>15, "coloured gems"=>12,
"traditional"=>0, "traditional & imitation"=>0}
第二部分通过对相同的键求和来合并它们。您的示例的结果将如下所示:
{"traditional and imitation"=>304, "chunky"=>34, "modern"=>15,
"coloured gems"=>12, "traditional"=>0, "traditional & imitation"=>0,
"alloy"=>161, "metal alloy"=>132, "metal"=>60, "925 sterling silver"=>8,
"lac"=>3, "beads"=>2, "beaded"=>0, "brass"=>0, "copper"=>0, "crystal"=>3,
"fabric"=>0, "feather"=>0, "glass"=>0, "jute"=>0, "leather"=>0, "pashmina"=>0,
"plastic"=>0, "polymer beads"=>0, "pu leather"=>0, "rexin"=>0, "rubber"=>0,
"satin"=>0, "shell"=>0, "silk"=>0, "silk brocade"=>0, "silver"=>39,
"silver alloy"=>0, "stainless steel"=>0, "sterling silver"=>0, ... }
答案 1 :(得分:1)
答案是:
Hash[@facet_counts["facet_fields"].map { |k, v| [k, Hash[*v]] }]