Skip to content

Instantly share code, notes, and snippets.

@jineeshjohn
Created December 2, 2016 01:03
Show Gist options
  • Save jineeshjohn/a0a105863db08befc365e6b7534fd185 to your computer and use it in GitHub Desktop.
Save jineeshjohn/a0a105863db08befc365e6b7534fd185 to your computer and use it in GitHub Desktop.
// Chaining
var lyrics=[
{line:1,words:"Im a lumberjack and Im okay"},
{line:2,words:"I sleep all night and I work all day"},
{line:3,words:"Hes a lumberjack and hes okay"},
{line:4,words:"He sleeps all night and he works all day"}
];
var values = _(lyrics).chain()
.map(function(line) {
return line.words.split();
})
.flatten()
.reduce(function(counts, word) {
counts[word] = (counts[word] || 0) + 1;
return counts;
}, {})
.value();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment