Created
April 16, 2020 12:59
-
-
Save beefchimi/1256530e9870abbc922a8c92ed38b91a to your computer and use it in GitHub Desktop.
Array Reduce Examples
This file contains 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
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