将嵌套哈希转换为单个哈希

时间:2018-05-29 00:00:43

标签: arrays ruby hash nested

我想转换此示例数据:

{"2018-05-16"=>{
  "ABCD"=>{929119=>0.14174555, 338296=>0.13858019, 332058=>0.13680765, 449614=>0.13679536}}}

进入这个哈希的单个数组:

[
  {"target_date"=>"2018-05-16", "location"=>"ABCD", "id"=>929119, "value"=>0.14174555},
  {"target_date"=>"2018-05-16", "location"=>"ABCD", "id"=>338296, "value"=>0.13858019},
  {"target_date"=>"2018-05-16", "location"=>"ABCD", "id"=>332058, "value"=>0.13680765},
  {"target_date"=>"2018-05-16", "location"=>"ABCD", "id"=>449614, "value"=>0.13679536}
]

1 个答案:

答案 0 :(得分:1)

h = { "2018-05-16"=>{
        "ABCD"=>{
          929119=>0.14174555,
          338296=>0.13858019,
          332058=>0.13680765,
          449614=>0.13679536
        }
      }
    }

dat, v = h.first
  #=> ["2018-05-16", {"ABCD"=>{929119=>0.14174555, 338296=>0.13858019,
                               332058=>0.13680765, 449614=>0.13679536}}]
loc, f = v.first
  #=> ["ABCD", {929119=>0.14174555, 338296=>0.13858019, 332058=>0.13680765,
  #             449614=>0.13679536}]
g = {"target_date"=>dat, "location"=>loc }
  #=> {"target_date"=>"2018-05-16", "location"=>"ABCD"}
f.map { |k,v| g.merge("id"=>k, "value"=>v) }
  #=> [{"target_date"=>"2018-05-16", "location"=>"ABCD", "id"=>929119,
  #     "value"=>0.14174555},
  #    {"target_date"=>"2018-05-16", "location"=>"ABCD", "id"=>338296,
  #     "value"=>0.13858019},
  #    {"target_date"=>"2018-05-16", "location"=>"ABCD", "id"=>332058,
  #     "value"=>0.13680765},
  #    {"target_date"=>"2018-05-16", "location"=>"ABCD", "id"=>449614,
  #     "value"=>0.13679536}]

另一种方式 - 可以概括 - 遵循。

h.each_with_object([]) do |(k0,v0),arr|
  v0.each do |k1,v1|
    v1.each.map do |k2,v2| 
      arr << { "target_date"=>k0, "location"=>k1, "id"=>k2, "value"=>v2 }
    end
  end
end
  #=> <same as for first method>

请注意,这适用于具有多个日期和/或位置的哈希,例如以下内容。

{ "2018-05-16"=>{ "ABCD"=>{ 92=>0.14, 44=>0.13 }, "EFGH"=>{ 12=>0.24, 34=>0.23 } },
  "2018-05-17"=>{ "ABCD"=>{ 52=>0.34, 34=>0.33 }, "EFGH"=>{ 42=>0.44, 74=>0.43 } } }

对于此哈希,将返回以下数组。

[{"target_date"=>"2018-05-16", "location"=>"ABCD", "id"=>92, "value"=>0.14},
 {"target_date"=>"2018-05-16", "location"=>"ABCD", "id"=>44, "value"=>0.13},
 {"target_date"=>"2018-05-16", "location"=>"EFGH", "id"=>12, "value"=>0.24},
 {"target_date"=>"2018-05-16", "location"=>"EFGH", "id"=>34, "value"=>0.23},
 {"target_date"=>"2018-05-17", "location"=>"ABCD", "id"=>52, "value"=>0.34},
 {"target_date"=>"2018-05-17", "location"=>"ABCD", "id"=>34, "value"=>0.33},
 {"target_date"=>"2018-05-17", "location"=>"EFGH", "id"=>42, "value"=>0.44},
 {"target_date"=>"2018-05-17", "location"=>"EFGH", "id"=>74, "value"=>0.43}]