JSFiddle - React, Tailwind, and code Playground

by Wentz Wu

HTML

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div id="result"></div>

JavaScript

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"}
];
var m = _.map(lyrics,function(line) { return line.words.split(' '); });
alert(JSON.stringify(m));
var f = _.flatten(m);
alert(JSON.stringify(f));

var result = _.chain(lyrics)
  .map(function(line) { return line.words.split(' '); })
  .flatten()
  .reduce(function(counts, word) {
    counts[word] = (counts[word] || 0) + 1;
    return counts;
  }, {})
  .value();

_.each(result,function(lyric,index){
    $("#result").append(lyric+"="+index);
    $("#result").append("<br>");
});