Created
October 28, 2019 18:20
-
-
Save andersonfernandes/844b78782a8d7e92d3797e7c420ce795 to your computer and use it in GitHub Desktop.
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
list = [ | |
{ | |
id: 1, | |
name: 'ze', | |
age: 12, | |
app_id: 1, | |
url: 'www.1.com' | |
}, | |
{ | |
id: 1, | |
name: 'ze', | |
age: 12, | |
app_id: 2, | |
url: 'www.2.com' | |
}, | |
{ | |
id: 1, | |
name: 'ze', | |
age: 12, | |
app_id: 3, | |
url: 'www.3.com' | |
}, | |
{ | |
id: 2, | |
name: 'Marcos', | |
age: 14, | |
app_id: 1, | |
url: 'www.1.com' | |
} | |
] | |
list = list.each_with_object({}) do |user, grouped_list| | |
id = user[:id] | |
if grouped_list[id].nil? | |
grouped_list[id] = { | |
id: id, | |
name: user[:name], | |
age: user[:age], | |
applications: [{ id: user[:app_id], url: user[:url] }] | |
} | |
else | |
grouped_list[id][:applications] << { id: user[:app_id], url: user[:url] } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment