Created
January 30, 2020 15:48
-
-
Save munkacsitomi/b3230340415e4b940f4941f9ebafc1fd to your computer and use it in GitHub Desktop.
Examples of Array.prototype.reduce()
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
const numbers = [1, 2, 3, 4, 12, 50]; | |
const reducer = (accumulator, currentValue) => accumulator + currentValue; | |
const reducedNumbers = numbers.reduce((accumulator, currentValue) => accumulator + currentValue); | |
const multipliedNumbers = numbers.map(x => x * 2); | |
console.log(numbers.reduce(reducer)); // => 72 | |
console.log(reducedNumbers); // => 72 | |
console.log(multipliedNumbers); // => [ 2, 4, 6, 8, 24, 100 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment