Skip to content

Instantly share code, notes, and snippets.

@martinomburajr
Last active October 26, 2017 15:12
Show Gist options
  • Select an option

  • Save martinomburajr/83939ec5c3cf34d1bd1e817af3a841e1 to your computer and use it in GitHub Desktop.

Select an option

Save martinomburajr/83939ec5c3cf34d1bd1e817af3a841e1 to your computer and use it in GitHub Desktop.
reduce operator basics
// Example 1: Simple Reduce
console.log('Reduce');
//The Observable consumes an array of numbers and spews each number out individually to the reduce operator.
Observable.from([1, 2, 3, 4, 5])
//reduce accepts two parameters (accumulator,current,index) and an optional seed, which is just an initial value
//set to 0 in this case.
//the lambda function adds the current value to the accumulator and repeats for all numbers in the array
.reduce((accumulator, value, index) => (accumulator + value), 0)
.subscribe(response => {
console.log(response); //logs the response
});
//Output
//Reduce
//15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment