Skip to content

Instantly share code, notes, and snippets.

@abelyakin
Created June 29, 2017 14:57
Show Gist options
  • Save abelyakin/71de092747a01d88f3c530baa2598803 to your computer and use it in GitHub Desktop.
Save abelyakin/71de092747a01d88f3c530baa2598803 to your computer and use it in GitHub Desktop.
reduce and count array elements to object
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