I have a Ruby hash which looks like this-
report = {
"count_by_state"=>[
{"day"=>"2021-02-23", "policy_state"=>2, "count"=>48},
{"day"=>"2021-02-23", "policy_state"=>3, "count"=>5},
{"day"=>"2021-02-23", "policy_state"=>4, "count"=>89},
{"day"=>"2021-02-25", "policy_state"=>2, "count"=>1},
{"day"=>"2021-02-25", "policy_state"=>3, "count"=>4},
{"day"=>"2021-02-25", "policy_state"=>4, "count"=>90}
]
}
I am trying to get a hash which looks like this
{2: {"2021-02-23" => 48,"2021-02-25" => 1},
3: {"2021-02-23" => 5,"2021-02-25" => 4},
4: {"2021-02-23" => 89,"2021-02-25" => 90}}
I have tried a bunch of things and have got so far with this code-
grouping = report['count_by_state'].group_by {|x| x['policy_state']}
puts grouping
puts grouping.map { |k,v| [ k => v.group_by {|x| x['day']} ] }
{2=>{"2021-02-23"=>[{"day"=>"2021-02-23", "policy_state"=>2, "count"=>48}], "2021-02-25"=>[{"day"=>"2021-02-25", "policy_state"=>2, "count"=>1}]}}
{3=>{"2021-02-23"=>[{"day"=>"2021-02-23", "policy_state"=>3, "count"=>5}], "2021-02-25"=>[{"day"=>"2021-02-25", "policy_state"=>3, "count"=>4}]}}
{4=>{"2021-02-23"=>[{"day"=>"2021-02-23", "policy_state"=>4, "count"=>89}], "2021-02-25"=>[{"day"=>"2021-02-25", "policy_state"=>4, "count"=>90}]}}
How can I get the values from day and count and create a value pair? I am trying to create a chart using squid and it uses data in a certain format.