Skip to content

Instantly share code, notes, and snippets.

@productioncoder
Last active December 17, 2018 15:04
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 productioncoder/7810d72555783300aa12c79bf44727d3 to your computer and use it in GitHub Desktop.
Save productioncoder/7810d72555783300aa12c79bf44727d3 to your computer and use it in GitHub Desktop.
Youtube videos reducer: group videos by id and category
function groupVideosByIdAndCategory(response) {
const videos = response.items;
const byId = {};
const byCategory = {
totalResults: response.pageInfo.totalResults,
nextPageToken: response.nextPageToken,
items: [],
};
videos.forEach((video) => {
byId[video.id] = video;
const items = byCategory.items;
if(items && items) {
items.push(video.id);
} else {
byCategory.items = [video.id];
}
});
return {byId, byCategory};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment