Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function createWordMap(Input) {
  2.   // count the number of appearances for each word
  3.   var wordsMap = {};
  4.   Input.forEach(function(key) {
  5.     if (wordsMap.hasOwnProperty(key)) {
  6.       wordsMap[key]++;
  7.     } else {
  8.       wordsMap[key] = 1;
  9.     }});
  10.   return wordsMap;
  11. }
  12.  
  13. function sortByCount(wordsMap) {
  14.   // create the map that will go into the .cvs file
  15.   var finalWordsArray = [];
  16.  
  17.   finalWordsArray = Object.keys(wordsMap).map(function(key) {
  18.     return {
  19.       name: key,
  20.       total: wordsMap[key]};
  21. });  
  22.   // sort by count in descending order
  23.   finalWordsArray.sort(function(a, b) {
  24.     return b.total - a.total;
  25.   });
  26.   return finalWordsArray;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement