[英]How to convert hash in array to array in ruby
I have array that element is hash 我有数组元素是哈希
a = [{:history_date=>"15/07/10"}, {:open_price=>"7.90"}]
I want to convert to this 我想转换成这个
h = {:history_date=>"15/07/10", :open_price=>"7.90"}
someone please help me. 有人请帮助我。
How about: 怎么样:
h = a.inject(&:merge)
Each hash in the array is merged into the previous hash until we get down to one element. 数组中的每个哈希都合并到前一个哈希中,直到我们得到一个元素。 &:merge
is shorthand for the following, which may be easier to understand, but is slightly longer: &:merge
是以下的简写,可能更容易理解,但稍长:
h = a.inject { |all, element| all.merge(element) }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.