将哈希键转换为ruby中的哈希值

时间:2017-09-12 17:53:08

标签: ruby-on-rails ruby

请查看以下输入,并帮助我将其转换为以下输出

输入:

arr = [{"Key1"=>1, "key3"=>1}, {"Key2"=>3, "Key4"=>0}, {"Key1"=>1, "Key4"=>1},
       {"Key2"=>3, "key3"=>0}, {"Key1"=>3, "Ke5"=>0}]

期望的输出:

[{"Key1"=>1}, {"Key2"=>1}, {"Key3"=>3}, {"Key4"=>0}, {"Key1"=>1},
 {"Key4"=>1}, {"Key3"=>3}, {"Key2"=>0}, {"Key1"=>3}, {"Key5"=>0}]

1 个答案:

答案 0 :(得分:2)

试试这个:

data.flat_map(&:to_a).map { |k, v| {k => v} }