Skip to content

Instantly share code, notes, and snippets.

@ofca
Created August 16, 2013 08:06
Show Gist options
  • Save ofca/6248144 to your computer and use it in GitHub Desktop.
Save ofca/6248144 to your computer and use it in GitHub Desktop.
// Input array
var input = [
{ id: 1, name: 'Tom', status: 'Accepted', category: 'Admin' },
{ id: 2, name: 'Chris', status: 'Not accepted', category: 'Moderator' },
{ id: 3, name: 'July', status: 'Accepted', category: 'Moderator' },
{ id: 4, name: 'John', status: 'To verify', category: 'Admin' },
{ id: 5, name: 'Jeff', status: 'Accepted', category: 'Admin' },
{ id: 6, name: 'Andrew', status: 'Accepted', category: 'Admin' },
];
// Output array grouped by category
[
{ name: 'Admin', children: [
{ id: 1, name: 'Tom', status: 'Accepted', category: 'Admin' },
{ id: 1, name: 'John', status: 'To verify', category: 'Admin' },
{ id: 5, name: 'Jeff', status: 'Accepted', category: 'Admin' },
{ id: 6, name: 'Andrew', status: 'Accepted', category: 'Admin' }
]
},
{ name: 'Moderator', children: [
{ id: 2, name: 'Chris', status: 'Not accepted', category: 'Moderator' },
{ id: 3, name: 'July', status: 'Accepted', category: 'Moderator' },
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment