Skip to content

Instantly share code, notes, and snippets.

@cuipengfei
Created December 14, 2013 13:30
Show Gist options
  • Save cuipengfei/7959130 to your computer and use it in GitHub Desktop.
Save cuipengfei/7959130 to your computer and use it in GitHub Desktop.
functional js reduce
module.exports = function countWords(arr) {
return arr.reduce(function(prev, curr) {
prev[curr] = prev[curr] + 1 || 1
return prev
}, {})
}
module.exports = function countStrings(inputWords) {
return inputWords.reduce(function(acc, current) {
acc[current] = inputWords.filter(function(word) {
return word === current
}).length
return acc
}, {})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment