Rails - 从哈希数组中删除键

时间:2016-12-20 19:46:03

标签: ruby-on-rails arrays hash

我在表单中提交了一系列哈希值。我正在等待一个开头的唯一键,因为字段的名称是相同的,我需要阻止它们互相覆盖。但是当我在一个字段中保存序列化的数据时,我不再需要密钥了,所以我正在寻找最好的方法来解决它:

{"6"=>{"Between Wires"=>"0"},
 "7"=>{"Wires to GND"=>"0"},
 "8"=>{"Between Wires"=>"0"},
 "9"=>{"Wires to GND"=>"0"},
 "10"=>{"Between Wires"=>"0"},
 "11"=>{"Wires to GND"=>"0"},
 "13"=>{"Between Wires"=>"0"},
 "14"=>{"Wires to GND"=>"0"},
 "16"=>{"Between Wires"=>"0"},
 "17"=>{"Wires to GND"=>"0"},
 "19"=>{"Between Wires"=>"0"}}

如何删除密钥以便我拥有它?

{{"Between Wires"=>"0"},
 {"Wires to GND"=>"0"},
 {"Between Wires"=>"0"},
 {"Wires to GND"=>"0"},
 {"Between Wires"=>"0"},
 {"Wires to GND"=>"0"},
 {"Between Wires"=>"0"},
 {"Wires to GND"=>"0"},
 {"Between Wires"=>"0"},
 {"Wires to GND"=>"0"},
 {"Between Wires"=>"0"},
 {"Wires to GND"=>"0"}}

2 个答案:

答案 0 :(得分:2)

已经回答了,但我相信更好的解决方案是使用#values

hash = {
 "6"=>{"Between Wires"=>"0"},
 "7"=>{"Wires to GND"=>"0"},
 "8"=>{"Between Wires"=>"0"}
}

hash.values

答案 1 :(得分:0)

您可以使用map对其进行迭代,然后省略密钥。

old = {"6"=>{"Between Wires"=>"0"}, "7"=>{"Wires to GND"=>"0"}}
new = old.map { |k,v| v }