我有一个哈希数组:
[{:key=>"RepositoryKey", :value=>"6958127"},
{:key=>"Guid", :value=>"006444a2-3ea1-4b64-81b8-51ce4756d337"},
{:key=>"Association_Guid", :value=>"05500000-93ba-4898-8308-444a25df7eb6"},
{:key=>"Association_Type", :value=>"Association"},
{:key=>"Association_Name", :value=>"Hancock Wellness Center"},
{:key=>"Image_Guid", :value=>nil},
{:key=>"Image_Type", :value=>"StoredFile"}]
这来自外部源,我需要将某些值组合成一个哈希。例如,我想从上面的数组中RepositoryKey
,Guid
和Association_Type
。我最终想要这样的东西:
hash = {"RepositoryKey" => "6958127",
"Guid" => "006444a2-3ea1-4b64-81b8-51ce4756d337",
"Association_Type" => "Association"}
使用Ruby 2.0.0的最佳方式是什么?
答案 0 :(得分:3)
array_of_hash = [
{:key=>"RepositoryKey", :value=>"6958127"},
{:key=>"Guid", :value=>"006444a2-3ea1-4b64-81b8-51ce4756d337"},
{:key=>"Association_Guid", :value=>"05500000-93ba-4898-8308-444a25df7eb6"},
{:key=>"Association_Type", :value=>"Association"},
{:key=>"Association_Name", :value=>"Hancock Wellness Center"},
{:key=>"Image_Guid", :value=>nil},
{:key=>"Image_Type", :value=>"StoredFile"}
]
array = ["RepositoryKey", "Guid", "Association_Type"]
Hash[array_of_hash.select { |h| array.include?(h[:key]) }.map(&:values)]
# => {"RepositoryKey"=>"6958127",
# "Guid"=>"006444a2-3ea1-4b64-81b8-51ce4756d337",
# "Association_Type"=>"Association"}
Hash[array_of_hash.map(&:values)]
# => {"RepositoryKey"=>"6958127",
# "Guid"=>"006444a2-3ea1-4b64-81b8-51ce4756d337",
# "Association_Guid"=>"05500000-93ba-4898-8308-444a25df7eb6",
# "Association_Type"=>"Association",
# "Association_Name"=>"Hancock Wellness Center",
# "Image_Guid"=>nil,
# "Image_Type"=>"StoredFile"}
如果您使用的是Ruby 2.1版本
array_of_hash.map(&:values).to_h
奖金:
如果每个内部哈希的键数多于示例1,并且您希望获得:key
和:value
的值,则写如下:
array_of_hash.map { |h| h.values_at(:key, :value) }.to_h
答案 1 :(得分:1)
早期答案的变体:
<强>代码强>
arr.each_with_object({}) { |h,g| g[h.values.first] = h.values.last }
.select { |k,_| target.include?(k) }
<强>演示强>
arr = [
{:key=>"RepositoryKey", :value=>"6958127"},
{:key=>"Guid", :value=>"006444a2-3ea1-4b64-81b8-51ce4756d337"},
{:key=>"Association_Guid", :value=>"05500000-93ba-4898-8308-444a25df7eb6"},
{:key=>"Association_Type", :value=>"Association"},
{:key=>"Association_Name", :value=>"Hancock Wellness Center"},
{:key=>"Image_Guid", :value=>nil},
{:key=>"Image_Type", :value=>"StoredFile"}
]
target = ["RepositoryKey", "Guid", "Association_Type"]
arr.each_with_object({}) { |h,g| g[h.values.first] = h.values.last }
.select { |k,_| target.include?(k) }
#=> {"RepositoryKey"=>"6958127",
# "Guid"=>"006444a2-3ea1-4b64-81b8-51ce4756d337",
# "Association_Type"=>"Association"}
答案 2 :(得分:0)
试试这个
array = [{:key=>"RepositoryKey", :value=>"6958127"},
{:key=>"Guid", :value=>"006444a2-3ea1-4b64-81b8-51ce4756d337"},
{:key=>"Association_Guid", :value=>"05500000-93ba-4898-8308-444a25df7eb6"},
{:key=>"Association_Type", :value=>"Association"},
{:key=>"Association_Name", :value=>"Hancock Wellness Center"},
{:key=>"Image_Guid", :value=>nil},
{:key=>"Image_Type", :value=>"StoredFile"}]
hash = Hash[array.map{|h| [h[:key],h[:value]]}]
#=> {"RepositoryKey"=>"6958127", "Guid"=>"006444a2-3ea1-4b64-81b8-51ce4756d337", "Association_Guid"=>"05500000-93ba-4898-8308-444a25df7eb6", "Association_Type"=>"Association", "Association_Name"=>"Hancock Wellness Center", "Image_Guid"=>nil, "Image_Type"=>"StoredFile"}
两个世界中最好的完整哈希,你可以拒绝你以后不想要的值
wanted_values = ["RepositoryKey", "Guid", "Association_Type"]
hash.reject{|k,v| !wanted_values.include?(k)}
#=>{"RepositoryKey"=>"6958127", "Guid"=>"006444a2-3ea1-4b64-81b8-51ce4756d337","Association_Type"=>"Association"}
hash
#=>=> {"RepositoryKey"=>"6958127", "Guid"=>"006444a2-3ea1-4b64-81b8-51ce4756d337", "Association_Guid"=>"05500000-93ba-4898-8308-444a25df7eb6", "Association_Type"=>"Association", "Association_Name"=>"Hancock Wellness Center", "Image_Guid"=>nil, "Image_Type"=>"StoredFile"}
答案 3 :(得分:0)
array_of_hash = [
{:key=>"RepositoryKey", :value=>"6958127"},
{:key=>"Guid", :value=>"006444a2-3ea1-4b64-81b8-51ce4756d337"},
{:key=>"Association_Guid", :value=>"05500000-93ba-4898-8308-444a25df7eb6"},
{:key=>"Association_Type", :value=>"Association"},
{:key=>"Association_Name", :value=>"Hancock Wellness Center"},
{:key=>"Image_Guid", :value=>nil},
{:key=>"Image_Type", :value=>"StoredFile"}
]
array = ["RepositoryKey", "Guid", "Association_Type"]
array_of_hash.select{|h| array.include? h[:key]}.map{|h|
Hash[*h.values]}.reduce Hash.new, :merge
#=> {"RepositoryKey"=>"6958127", "Guid"=>"006444a2-3ea1-4b64-81b8-51ce4756d337", "As#ociation_Type"=>"Association"}