Created
February 29, 2020 06:11
-
-
Save b2977053/79b02cf1029bad29c580cfc267347468 to your computer and use it in GitHub Desktop.
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
//1. 正常 情況 | |
var people = [ | |
{name: 'Anna', score: 100, subject: 'English'}, | |
{name: 'Anna', score: 90, subject: 'Math'}, | |
{name: 'Anna', score: 96, subject: 'Chinese' }, | |
{name: 'Jerry', score: 80, subject: 'English'}, | |
{name: 'Jerry', score: 100, subject: 'Math'}, | |
{name: 'Jerry', score: 90, subject: 'Chinese' }, | |
]; | |
var source = Rx.Observable | |
.from(people) | |
.zip(Rx.Observable.interval(100),(x, y) => x); | |
var example = source | |
.groupBy(person => person.name) | |
.map(group => group.reduce((acc, curr) => ({ | |
name: curr.name, | |
score: curr.score + acc.score | |
}))) | |
.mergeAll(); | |
example.subscribe(console.log); |
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
//2. 加上.take(3) 變更 people 順序,為什麼輸出多出subject欄位 | |
var people = [ | |
{name: 'Anna', score: 100, subject: 'English'}, | |
{name: 'Jerry', score: 80, subject: 'English'}, | |
{name: 'Anna', score: 90, subject: 'Math'}, | |
{name: 'Anna', score: 96, subject: 'Chinese' }, | |
{name: 'Jerry', score: 100, subject: 'Math'}, | |
{name: 'Jerry', score: 90, subject: 'Chinese' }, | |
]; | |
var source = Rx.Observable | |
.from(people) | |
.zip(Rx.Observable.interval(100).take(3),(x, y) => x); | |
var example = source | |
.groupBy(person => person.name) | |
.map(group => group.reduce((acc, curr) => ({ | |
name: curr.name, | |
score: curr.score + acc.score | |
}))) | |
.mergeAll(); | |
example.subscribe(console.log); |
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
//3. 把 mergeAll 換成 concatAll,為什麼只輸出一個 | |
var people = [ | |
{name: 'Anna', score: 100, subject: 'English'}, | |
{name: 'Anna', score: 90, subject: 'Math'}, | |
{name: 'Anna', score: 96, subject: 'Chinese' }, | |
{name: 'Jerry', score: 80, subject: 'English'}, | |
{name: 'Jerry', score: 100, subject: 'Math'}, | |
{name: 'Jerry', score: 90, subject: 'Chinese' }, | |
]; | |
var source = Rx.Observable | |
.from(people) | |
.zip(Rx.Observable.interval(100),(x, y) => x); | |
var example = source | |
.groupBy(person => person.name) | |
.map(group => group.reduce((acc, curr) => ({ | |
name: curr.name, | |
score: curr.score + acc.score | |
}))) | |
.concatAll(); | |
example.subscribe(console.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment