JSFiddle - React, Tailwind, and code Playground

by mariocatch

HTML

<pre id="data">
  Run the code below
</pre>

JavaScript

function countWords(wordList) {
  var index = {};

  wordList.forEach(function(word) {
    if (!index[word]) index[word] = 0;
    index[word]++;
  });

  return Object.keys(index).map(function(word) {
    return {
      word: word,
      weight: index[word]
    };
  });
}

//----------------------------------------------------------------------------

var wordData = countWords(['a', 'b', 'c', 'a', 'c', 'c']);
document.getElementById('data').innerHTML = JSON.stringify(wordData, null, 2);