max

The max() operator finds the largest value. This comes in two flavors: we either just call the max() operator with no arguments, or we give it a compare function. The compare function then decides whether something is larger than, smaller than, or equal to an emitted value. Let's have a look at the two different versions:

// mathematical/max.js

let streamWithNumbers$ = Rx.Observable
.of(1,2,3,4)
.max();

// 4
streamWithNumbers$.subscribe(data => console.log(data));

function comparePeople(firstPerson, secondPerson) {
if (firstPerson.age > secondPerson.age) {
return 1;
} else if (firstPerson.age < secondPerson.age) {
return -1;
}
return 0;
}

let streamOfObjects$ = Rx.Observable
.of({
name : "Yoda",
age: 999
}, {
name : "Chris",
age: 38
})
.max(comparePeople);

// { name: 'Yoda', age : 999 }
streamOfObjects$.subscribe(data => console.log(data));

We can see in the preceding code that we get one result back and it is the largest one.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.115.118