Skip to content

Instantly share code, notes, and snippets.

@dkam
Last active January 20, 2025 11:42
Show Gist options
  • Save dkam/525ac4177964549ee2f2ca7febd03eea to your computer and use it in GitHub Desktop.
Save dkam/525ac4177964549ee2f2ca7febd03eea to your computer and use it in GitHub Desktop.
Uniquify Array
# 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://gist.github.com/dkam/525ac4177964549ee2f2ca7febd03eea
# filename: uniquify_array.rb
# version: 0.1
# @PICOPACKAGE_END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment