Skip to content

Instantly share code, notes, and snippets.

@tonyonodi
Created May 5, 2016 21:04
Show Gist options
  • Save tonyonodi/246a82cd3c10b713405b6bf70417fe29 to your computer and use it in GitHub Desktop.
Save tonyonodi/246a82cd3c10b713405b6bf70417fe29 to your computer and use it in GitHub Desktop.
Functional way of reducing array to list.
// 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