简体   繁体   English

Ruby将Hash Key作为Hash + Key组合并作为数组返回

[英]Ruby Combine Hash Key As Hash + Key and return as array

Suppose I have a hash 假设我有一个哈希

hash = {"123"=>"abc", "124"=>"def"} 杂凑= {“ 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 [[“” 123-abc“,” 123“],[” 124-def“,” 124“]],这样我就可以将其传递给我的选择标签以显示“ 123-abc”并在选择时提交ID

I came up with a solution 我想出了一个解决方案

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

But is there a better way other than this? 但是还有其他更好的方法吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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