Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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