Extracting Data from array of hashes Ruby

Given that I have the following array of hashes

@response =  { "0"=>{"forename_1"=>"John", "surname_1"=>"Smith"}, 
               "1"=>{"forename_1"=>"Chris", "surname_1"=>"Jenkins"}, 
               "2"=>{"forename_1"=>"Billy", "surname_1"=>"Bob"},
               "Status" => 100
             }

I am looking to create an array of the forename_1 and surname_1 values combined, so desired output would be

["John Smith","Chris Jenkins","Billy Bob"]

--------------------------------------------------------

@response.values.grep(Hash).map { |h| "#{h['forename_1']} #{h['surname_1']}" }
# => ["John Smith", "Chris Jenkins", "Billy Bob"]

or

@response.values.grep(Hash).map { |h| h['forename_1']+' '+h['surname_1'] }

or

@response.values.grep(Hash).map { |t| t.values.join(' ')}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章