0

我想根据所有三个字段从我的代码中删除重复项,即基于事件名称、日期和时间,如果所有三个字段都匹配两个事件,那么应该删除一个仅显示唯一事件的代码。我编写的代码有效仅适用于一个字段,即基于事件名称的删除条目。我如何匹配所有三个字段。我正在使用 & 但没有成功。

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
4

1 回答 1

4

使用uniq方法Array

list.uniq
于 2012-07-18T07:06:47.720 回答