简体   繁体   中英

Ruby Combine Hash Key As Hash + Key and return as array

Suppose I have a hash

hash = {"123"=>"abc", "124"=>"def"}

I'm expecting output

[["123 - abc", "123"], ["124 - def", "124"]] so that I could pass it to my select tag to display the "123 - abc" and to submit the id when selected

I came up with a solution

hash.map{|key,value| ["#{key} - #{value}",key]}

But is there a better way other than this?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM