0
我想基於所有三個字段,即基於事件名稱,日期和時間從我的代碼中刪除重複項,如果所有三個字段都匹配兩個事件,則應刪除一個,只顯示唯一events。我寫的代碼僅適用於一個字段,即根據eventname刪除條目。如何匹配所有三個字段。我正在使用&,但沒有成功。在ruby中刪除重複的數據
include Enumerable
list = [
{
:eventname => "music show",
:date => "1st august",
:time => "9 pm"
},
{
:eventname => "dance show",
:date => "11st august",
:time => "9 pm"
},
{
:eventname => "music show",
:date => "1st august",
:time => "9 pm"
},
{
:eventname => "music show",
:date => "15st august",
:time => "9 pm"
},
{
:eventname => "magic show",
:date => "12st august",
:time => "9 pm"
},
{
:eventname => "rock show",
:date => "1st august",
:time => "9 pm"
}
]
b=list.group_by{|r| r[:eventname]}.map do |k, v|
v.inject({}) { |r, h| r.merge(h){ |key, o, n| o || n } }
end