Created
December 14, 2013 13:30
-
-
Save cuipengfei/7959130 to your computer and use it in GitHub Desktop.
functional js reduce
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
module.exports = function countWords(arr) { | |
return arr.reduce(function(prev, curr) { | |
prev[curr] = prev[curr] + 1 || 1 | |
return prev | |
}, {}) | |
} |
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
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