作者热门文章
- android - AmazonSNS - AwsCredentials.properties - NullPointerException
- android - ListView invalidate() 与 invalidateViews() 之间的区别?
- android - 获取上下文的问题
- Android M 权限模型
我有以下内容
friends = [{ name: "Jack", attr1:"def", attr2:"def" }, { name: "Jill", attr1:"def", attr2:"def" }]
我想把上面的表示转换成这样的哈希的哈希
friends = { "Jack" => { attr1: "def", attr2:"def" }, "Jill" => { attr1: "def", attr2: "def" } }
在 Ruby 中有什么优雅的方法可以做到这一点?
最佳答案
Hash[friends.map { |f| _f = f.dup; [_f.delete(:name), _f] }]
# => {"Jack"=>{:attr1=>"def", :attr2=>"def"}, "Jill"=>{:attr1=>"def", :attr2=>"def"}}
关于 ruby : Generate a Hash of Hashes from an Array of Hashes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11230022/
我是一名优秀的程序员,十分优秀!