我有一个散列数组,如下所示:
[{"k1"=>"v1", "k2"=>"75.1%"}, {"k1"=>"v2", "k2"=>"-NA-"}, {"k1"=>"v3", "k2"=>"5.1%"}]
现在,我想首先检查数组是否包含一个键
"k1"
值"v3"
的散列。如果是,那么我想从数组中删除这个散列。结果应该是:
[{"k1"=>"v1", "k2"=>"75.1%"}, {"k1"=>"v2", "k2"=>"-NA-"}]
最佳答案
arr = [{"k1"=>"v1", "k2"=>"75.1%"}, {"k1"=>"v2", "k2"=>"-NA-"}, {"k1"=>"v3", "k2"=>"5.1%"}]
arr.delete_if { |h| h["k1"] == "v3" }
#=> [{"k1"=>"v1", "k2"=>"75.1%"}, {"k1"=>"v2", "k2"=>"-NA-"}]
如果没有与条件匹配的哈希,则数组保持不变。
关于ruby - 从Ruby中的哈希数组删除哈希,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19218208/