Last active
January 20, 2025 11:42
-
-
Save dkam/525ac4177964549ee2f2ca7febd03eea to your computer and use it in GitHub Desktop.
Uniquify Array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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