Skip to content

Instantly share code, notes, and snippets.

@victusfate
Created January 29, 2011 17:25
Show Gist options
  • Select an option

  • Save victusfate/802013 to your computer and use it in GitHub Desktop.

Select an option

Save victusfate/802013 to your computer and use it in GitHub Desktop.
var lyrics = [
{line : 1, words : "I'm a lumberjack and I'm okay"},
{line : 2, words : "I sleep all night and I work all day"},
{line : 3, words : "He's a lumberjack and he's okay"},
{line : 4, words : "He sleeps all night and he works all day"}
];
_(lyrics).chain()
.map(function(line) { return line.words.split(' '); })
.flatten()
.reduce(function(counts, word) {
counts[word] = (counts[word] || 0) + 1;
return counts;
}, {}).value();
=> {lumberjack : 2, all : 4, night : 2 ... }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment