-
-
Save ilmsg/2bb4c37c5f14e5622eeb53b723b56796 to your computer and use it in GitHub Desktop.
Using Map-Reduce in Javascript: counting words in a string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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