Created
June 29, 2017 14:57
-
-
Save abelyakin/71de092747a01d88f3c530baa2598803 to your computer and use it in GitHub Desktop.
reduce and count array elements to object
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
function countWords(arr) { | |
return arr.reduce(function(countMap, word) { | |
countMap[word] = ++countMap[word] || 1 // increment or initialize to 1 | |
return countMap | |
}, {}) // second argument to reduce initialises countMap to {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment