Find keep duplicates in Ruby hashes

前端 未结 1 1072
长发绾君心
长发绾君心 2021-01-12 20:57

I have an array of hashes where I need to find and store matches based on one matching value between the hashes.

a = [{:id => 1, :name => \"Jim\", :em         


        
相关标签:
1条回答
  • 2021-01-12 21:59

    I tested this and it will do exactly what you want:

    b = a.group_by { |h| h[:name] }.values.select { |a| a.size > 1 }.flatten
    

    However, you might want to look at some of the intermediate objects produced in that calculation and see if those are more useful to you.

    0 讨论(0)
提交回复
热议问题