Last active
October 26, 2017 15:12
-
-
Save martinomburajr/83939ec5c3cf34d1bd1e817af3a841e1 to your computer and use it in GitHub Desktop.
reduce operator basics
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
| // 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