Skip to content

Instantly share code, notes, and snippets.

@munkacsitomi
Created January 30, 2020 15:48
Show Gist options
  • Save munkacsitomi/b3230340415e4b940f4941f9ebafc1fd to your computer and use it in GitHub Desktop.
Save munkacsitomi/b3230340415e4b940f4941f9ebafc1fd to your computer and use it in GitHub Desktop.
Examples of Array.prototype.reduce()
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