hello.rb
· 1.6 KiB · Ruby
Bruto
# 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
# @PICOPACKAGE_START
# ---
# source_url: https://demo.opengist.io/demo/055d22e580e24e3797bca862cefbea7e
# filename: uniquify_array.rb
# version: 0.1
# content_checksum: sha256:806744e83b59e741f0c6e7f342a7d188e4af04cd44145fe2381c511e49b67b63
# @PICOPACKAGE_END
1 | # Make an array of hashes contain a unique value for key: |
2 | def uniquify_array(array, key) |
3 | array.group_by { |item| item[key] }.map do |_, items| |
4 | (items.length > 1) ? yield(items) : items.first |
5 | end |
6 | end |
7 | |
8 | |
9 | # new_array = uniquify_array(array, :some_key) do |dupes| |
10 | # dupes.max_by { |item| item[:updated_at] } |
11 | # end |
12 | |
13 | # @PICOPACKAGE_START |
14 | # --- |
15 | # source_url: https://demo.opengist.io/demo/055d22e580e24e3797bca862cefbea7e |
16 | # filename: uniquify_array.rb |
17 | # version: 0.1 |
18 | # content_checksum: sha256:806744e83b59e741f0c6e7f342a7d188e4af04cd44145fe2381c511e49b67b63 |
19 | # @PICOPACKAGE_END |