我知道,如果您有一个 array
并将其引用为 array.uniq
,它将返回而没有任何重复项。
然而,在这种情况下,它是一个对象数组(是正确的 ruby 语言吗?)。我希望每次调用都进入 @calls
数组,除非 call.from
与数组中已存在的 call_formatted 对象相同。
如果数组中没有其他对象具有相同的 call.from
值,我如何有条件地将这些对象放入数组中?
calls_raw.each do |call|
call_formatted = {
:date => date,
:time => time,
:from => call.from,
:duration => call.duration,
:recording => recording,
}
@calls << call_formatted
end
最佳答案
array.uniq { |item| item[:from] }
关于ruby-on-rails - 用于将唯一项插入数组的 Ruby 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31598296/