Created
May 5, 2016 21:04
-
-
Save tonyonodi/246a82cd3c10b713405b6bf70417fe29 to your computer and use it in GitHub Desktop.
Functional way of reducing array to list.
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
// Not really sure how I feel about this but it's kinda cool... | |
const prettyList = function(list, conjunction) { | |
return list.reduce((acc, str, i, arr) => ( | |
arr.length - i == 1 ? | |
`${acc}${conjunction} "${str}"` : | |
`${acc}"${str}", ` | |
), | |
""); | |
} | |
prettyList(["apples", "pears", "oranges", "bananas"], "and") // "apples", "pears", "oranges", and "bananas" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment