Skip to content

Instantly share code, notes, and snippets.

@bradoyler
Last active November 6, 2022 01:45
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bradoyler/5610057 to your computer and use it in GitHub Desktop.
Save bradoyler/5610057 to your computer and use it in GitHub Desktop.
Using Map-Reduce in Javascript: counting words in a string.
// for counting words in a string
var words="Hi there and hello there. Welcome and hello there.";
var wordcnt = words.replace(/[^\w\s]/g, "").split(/\s+/).reduce(function(map, word){
map[word] = (map[word]||0)+1;
return map;
}, Object.create(null));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment