Skip to content

Instantly share code, notes, and snippets.

@himynameisdave
Last active May 21, 2017 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save himynameisdave/a3956cc8a2df96bff300b909d79c8155 to your computer and use it in GitHub Desktop.
Save himynameisdave/a3956cc8a2df96bff300b909d79c8155 to your computer and use it in GitHub Desktop.
Reduce your fears about .reduce() - Reduce array to single string
/*
Naive implementation
*/
let everyonesName = '';
users.forEach(user => {
everyonesName += `${user.firstName} ${user.lastName}\n`;
});
/*
Better one-line-using-reduce implementation
*/
const everyonesName = users.reduce((acc, user) => `${acc}${user.firstName} ${user.lastName}\n`, '');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment