[英]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.