hello.rb
· 413 B · Ruby
Raw
# @META_START
# source_url: https://
# filename: uniquify_array.rb
# version: 0.1
# @META_END
# Make an array of hashes contain a unique value for key:
def uniquify_array(array, key)
array.group_by { |item| item[key] }.map do |_, items|
(items.length > 1) ? yield(items) : items.first
end
end
# new_array = uniquify_array(array, :some_key) do |dupes|
# dupes.max_by { |item| item[:updated_at] }
# end
1 | # @META_START |
2 | # source_url: https:// |
3 | # filename: uniquify_array.rb |
4 | # version: 0.1 |
5 | # @META_END |
6 | |
7 | # Make an array of hashes contain a unique value for key: |
8 | def uniquify_array(array, key) |
9 | array.group_by { |item| item[key] }.map do |_, items| |
10 | (items.length > 1) ? yield(items) : items.first |
11 | end |
12 | end |
13 | |
14 | |
15 | # new_array = uniquify_array(array, :some_key) do |dupes| |
16 | # dupes.max_by { |item| item[:updated_at] } |
17 | # end |