您的位置:首页 > 编程语言 > Ruby

Ruby converting array of hashes to array of arrays

2014-09-26 09:00 375 查看
I have a ruby array of hashes
arr1 = [:a => {:name=>"Bob",:age=>"10",:city=>"NY"},
:b => {:name=>"Mike",:age=>"20",:city=>"FL"}]


What is the best way to convert this to
arr2 = [["Bob",10],["Mike",20]]


in ruby.

Use map.
arr1.first.values.map{|h| [h[:name], h[:age].to_i]}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: