如何根据值从哈希中删除重复的条目

时间:2015-06-08 05:32:41

标签: ruby-on-rails ruby hash

我有一个包含重复条目的列表。我需要一个包含基于unit_id的唯一条目的列表。

hash_list = {
  "a"=>{"unit_id"=>"43", "dep_id"=>"153", "_destroy"=>"false"},
  "b"=>{"unit_id"=>"43", "dep_id"=>"153", "_destroy"=>"false"},
  "c"=>{"unit_id"=>"43", "dep_id"=>"154", "_destroy"=>"false"},
  "d"=>{"unit_id"=>"42", "dep_id"=>"154", "_destroy"=>"false"}
}

预期产出:

hash_list = {
  "a"=>{"unit_id"=>"43", "dep_id"=>"153", "_destroy"=>"false"},
  "d"=>{"unit_id"=>"42", "dep_id"=>"154", "_destroy"=>"false"}
}

2 个答案:

答案 0 :(得分:5)

hash_list.to_a
         .uniq! { |_, v| v['unit_id'] }
         .to_h

但请注意,仅基于密钥unit_id删除重复项。
要根据多个键进行操作,

hash_list.to_a
         .uniq! { |_, v| v.values_at('unit_id','_destroy') }
         .to_h

请查看Hash#values_at

<强>输出

>> hash_list = { "a"=>{"unit_id"=>"43", "dep_id"=>"153", "_destroy"=>"false"}, "b"=>{"unit_id"=>"43", "dep_id"=>"153", "_destroy"=>"1"}, "c"=>{"unit_id"=>"43", "dep_id"=>"154", "_destroy"=>"false"}, "d"=>{"unit_id"=>"42", "dep_id"=>"154", "_destroy"=>"false"} }
#=> {"a"=>{"unit_id"=>"43", "dep_id"=>"153", "_destroy"=>"false"}, "b"=>{"unit_id"=>"43", "dep_id"=>"153", "_destroy"=>"1"}, "c"=>{"unit_id"=>"43", "dep_id"=>"154", "_destroy"=>"false"}, "d"=>{"unit_id"=>"42", "dep_id"=>"154", "_destroy"=>"false"}}
>> hash_list.to_a.uniq! { |_, v| v.values_at('unit_id','_destroy') }.to_h
#=> {"a"=>{"unit_id"=>"43", "dep_id"=>"153", "_destroy"=>"false"}, "b"=>{"unit_id"=>"43", "dep_id"=>"153", "_destroy"=>"1"}, "d"=>{"unit_id"=>"42", "dep_id"=>"154", "_destroy"=>"false"}}

答案 1 :(得分:1)

这是一种不将哈希转换为数组,修改数组,然后将修改后的数组转换回哈希的方法:

require 'set'

s = Set.new
hash_list.select { |_,h| s.add?(h["unit_id"]) }
  #=> {"a"=>{"unit_id"=>"43", "dep_id"=>"153", "_destroy"=>"false"},
  #    "d"=>{"unit_id"=>"42", "dep_id"=>"154", "_destroy"=>"false"}}