将数组中的哈希值转换为哈希值中的哈希值

时间:2014-02-13 09:00:30

标签: ruby-on-rails ruby arrays hash

我把这个Hash放在一个数组中:

[{"Date"=>"2014-02-12", "All Installs"=>"7,226", "Bootups"=>"358,439"}, {"Date"=>"2014-02-11", "All Installs"=>"7,759", "Bootups"=>"383,873"}, {"Date"=>"2014-02-10", "All Installs"=>"7,958", "Bootups"=>"286,067"}, {"Date"=>"2014-02-09", "All Installs"=>"9,439", "Bootups"=>"331,402"}]

我需要将其转换为:哈希,其中每个哈希都有一个包含在其中的相同日期的密钥。

{"2014-02-12"=>{"Date"=>"2014-02-12", "All Installs"=>"7,226", "Bootups"=>"358,439"}, "2014-02-11"=>{"Date"=>"2014-02-11", "All Installs"=>"7,759", "Bootups"=>"383,873"}, "2014-02-10"=>{"Date"=>"2014-02-10", "All Installs"=>"7,759", "Bootups"=>"383,873"}, "2014-02-09"=> {"Date"=>"2014-02-09", "All Installs"=>"9,439", "Bootups"=>"331,402"}}

谢谢!

5 个答案:

答案 0 :(得分:1)

data =  [{"Date"=>"2014-02-12", "All Installs"=>"7,226", "Bootups"=>"358,439"}, {"Date"=>"2014-02-11", "All Installs"=>"7,759", "Bootups"=>"383,873"}, {"Date"=>"2014-02-10", "All Installs"=>"7,958", "Bootups"=>"286,067"}, {"Date"=>"2014-02-09", "All Installs"=>"9,439", "Bootups"=>"331,402"}]

data_hash = Hash[data.map{|h| [h['Date'], h]}]

如果你不再需要哈希中的日期,你可以这样做:

data_hash = Hash[data.map{|h| [h.delete('Date'), h]}] #=> {"2014-02-12"=>{"All Installs"=>"7,226", "Bootups"=>"358,439"}...

答案 1 :(得分:1)

您可以这样使用:

将数组赋值给变量

record = [{"Date"=>"2014-02-12", "All Installs"=>"7,226", "Bootups"=>"358,439"}, {"Date"=>"2014-02-11", "All Installs"=>"7,759", "Bootups"=>"383,873"}, {"Date"=>"2014-02-10", "All Installs"=>"7,958", "Bootups"=>"286,067"}, {"Date"=>"2014-02-09", "All Installs"=>"9,439", "Bootups"=>"331,402"}]

然后

Hash[record.map { |r| [r['Date'], r] }]

结果是:

{"2014-02-12"=>{"Date"=>"2014-02-12", "All Installs"=>"7,226", "Bootups"=>"358,439"}, "2014-02-11"=>{"Date"=>"2014-02-11", "All Installs"=>"7,759", "Bootups"=>"383,873"}, "2014-02-10"=>{"Date"=>"2014-02-10", "All Installs"=>"7,958", "Bootups"=>"286,067"}, "2014-02-09"=>{"Date"=>"2014-02-09", "All Installs"=>"9,439", "Bootups"=>"331,402"}}

答案 2 :(得分:0)

[{"Date"=>"2014-02-12", "All Installs"=>"7,226", "Bootups"=>"358,439"}, {"Date"=>"2014-02-11", "All Installs"=>"7,759", "Bootups"=>"383,873"}, {"Date"=>"2014-02-10", "All Installs"=>"7,958", "Bootups"=>"286,067"}, {"Date"=>"2014-02-09", "All Installs"=>"9,439", "Bootups"=>"331,402"}]
.map{|h| [h["Date"], h]}.to_h

答案 3 :(得分:0)

关于堆栈溢出的第一篇文章,这里什么都没有 - 这是一个简单的答案,并没有解决任何特殊情况。新哈希将存储在new_hash变量中!

original_array = [{"Date"=>"2014-02-12", "All Installs"=>"7,226", "Bootups"=>"358,439"}, {"Date"=>"2014-02-11", "All Installs"=>"7,759", "Bootups"=>"383,873"}, {"Date"=>"2014-02-10", "All Installs"=>"7,958", "Bootups"=>"286,067"}, {"Date"=>"2014-02-09", "All Installs"=>"9,439", "Bootups"=>"331,402"}]

new_hash = {}
original_array.each do |hash|
  hash.each do |key, value|
    if key == "Date"
        new_hash[value] = hash
    end
  end
end

答案 4 :(得分:0)

使用#index_by method in ActiveSupport

[{"Date"=>"2014-02-12", "All Installs"=>"7,226", "Bootups"=>"358,439"}, {"Date"=>"2014-02-11", "All Installs"=>"7,759", "Bootups"=>"383,873"}, {"Date"=>"2014-02-10", "All Installs"=>"7,958", "Bootups"=>"286,067"}, {"Date"=>"2014-02-09", "All Installs"=>"9,439", "Bootups"=>"331,402"}].index_by{|h| h["Date"]}

结果

{"2014-02-12"=>{"Date"=>"2014-02-12", "All Installs"=>"7,226", "Bootups"=>"358,439"}, "2014-02-11"=>{"Date"=>"2014-02-11", "All Installs"=>"7,759", "Bootups"=>"383,873"}, "2014-02-10"=>{"Date"=>"2014-02-10", "All Installs"=>"7,958", "Bootups"=>"286,067"}, "2014-02-09"=>{"Date"=>"2014-02-09", "All Installs"=>"9,439", "Bootups"=>"331,402"}}