Skip to content

Instantly share code, notes, and snippets.

@isaac-martin
Created February 5, 2019 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaac-martin/21b91d98e544d9791c32a40fce9f30f0 to your computer and use it in GitHub Desktop.
Save isaac-martin/21b91d98e544d9791c32a40fce9f30f0 to your computer and use it in GitHub Desktop.
groupData = posts => {
const array = Object.values(posts);
const res = array.reduce((acc, curr) => {
if (!acc[curr.data.subreddit]) {
acc[curr.data.subreddit] = [];
}
// If this subreddit wasn't previously stored create an empty array
acc[curr.data.subreddit].push(curr); // Add the post to group
return acc;
}, {});
return Object.values(res); // convert to array and return
};
// Group by key in data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment