简体   繁体   English

Rails-散列数组中的带状键

[英]Rails - strip key from array of hashes

I have an array of hashes I'm submitting in a form. 我有一系列以表格形式提交的哈希。 I'm pre-pending a unique key to the beginning because the names of the fields are the same and I needed to stop them over-writing each other. 我将一个唯一的键添加到开头,因为这些字段的名称相同,并且需要阻止它们相互覆盖。 But when I save the data serialized in a field, I don't need the key anymore, so I'm looking for the best way to take it out: 但是,当我将序列化的数据保存在字段中时,我不再需要密钥,因此我正在寻找取出数据的最佳方法:

{"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"}}

How can I remove the keys so I have this? 我该如何取出钥匙?

{{"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"}}

It's been already answered, but I believe a better solution would be to use #values 已经回答了,但是我相信更好的解决方案是使用#values

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

hash.values

You can iterate over them with map and leave out the key. 您可以使用map遍历它们,而忽略密钥。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM