Skip to content

Instantly share code, notes, and snippets.

@beefchimi
Created April 16, 2020 12:59
Show Gist options
  • Save beefchimi/1256530e9870abbc922a8c92ed38b91a to your computer and use it in GitHub Desktop.
Save beefchimi/1256530e9870abbc922a8c92ed38b91a to your computer and use it in GitHub Desktop.
Array Reduce Examples
const items = [1, 2, 3, 4, 5];
/* Example when we return only a string value. */
const concatString = items.reduce((string, item) => `${string}${item},`, "");
/* Example when we return the first odd number. */
const firstOddValue = items.reduce((value, item) => {
if (value == null && item % 2 === 0) {
return item;
}
return value
}, null);
console.log(concatString);
console.log(firstOddValue);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment