Skip to content

Instantly share code, notes, and snippets.

@andersonfernandes
Created October 28, 2019 18:20
Show Gist options
  • Save andersonfernandes/844b78782a8d7e92d3797e7c420ce795 to your computer and use it in GitHub Desktop.
Save andersonfernandes/844b78782a8d7e92d3797e7c420ce795 to your computer and use it in GitHub Desktop.
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