Skip to content

Instantly share code, notes, and snippets.

@artcommacode
Created May 21, 2014 13:19
Show Gist options
  • Save artcommacode/f9132accf1c491d2c3be to your computer and use it in GitHub Desktop.
Save artcommacode/f9132accf1c491d2c3be to your computer and use it in GitHub Desktop.
// Which is more 'functional'?
//FIRST
render.allTags = pages.reduce(function (allTags, page) {
page.tags.split(', ').forEach(function (tag) {
allTags[tag] = ++allTags[tag] || 1;
});
return allTags;
}, {});
//SECOND
render.allTags = pages.reduce(function (allTags, page) {
return allTags.concat(page.tags.split(', '));
}, []).reduce(function (countedTags, tag) {
countedTags[tag] = ++countedTags[tag] || 1;
return countedTags;
}, {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment