0

我有一个数组

[{:id=>1, :name=>"name"}, {:id=>2, :name=>"name2"}, {:id=>45, :name=>"name3"}]

将它变成哈希的优雅方法是什么?(像这样):

{1=>"name", 2=>"name2", 45=>"name3"}
4

1 回答 1

2
arr = [{:id=>1, :name=>"name"}, {:id=>2, :name=>"name2"}, {:id=>45, :name=>"name3"}]
hash = Hash[arr.map &:values]
# => {1=>"name", 2=>"name2", 45=>"name3"}

文档链接:Hash::[]

于 2013-01-15T14:41:43.443 回答