我有以下数组,我想选择每个键:
names = [
{"Ánias"=>{:gender=>"male", :nationality=>"faroese"}},
{"Annfinnur"=>{:gender=>"male", :nationality=>"faroese"}},
{"Ansgar"=>{:gender=>"male", :nationality=>"faroese"}}
]
我如何选择所有名字(“Ánias”,“Annfinnur”,“Ansgar”)?
答案 0 :(得分:2)
只做
names = [
{"Ánias"=>{:gender=>"male", :nationality=>"faroese"}},
{"Annfinnur"=>{:gender=>"male", :nationality=>"faroese"}},
{"Ansgar"=>{:gender=>"male", :nationality=>"faroese"}}
]
names.map { |h| h.keys.first }
# => ["Ánias", "Annfinnur", "Ansgar"]