W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
根據(jù)指定條件將 Observable 發(fā)出的項目分組, 并發(fā)出這些分組的項目為 GroupedObservables
,一個 GroupedObservable
每組。
groupBy<T, K, R>(keySelector: (value: T) => K, elementSelector?: void | ((value: T) => R), durationSelector?: (grouped: GroupedObservable
<K, R>) => Observable<any>, subjectSelector?: () => Subject
<R>): OperatorFunction
<T, GroupedObservable
<K, R>>
keySelector | 提取密鑰的功能 對于每個項目。 |
---|---|
elementSelector | 可選的。 默認(rèn)值為 undefined 。 提取 每個項目的返回元素。 |
durationSelector | 可選的。 默認(rèn)值為 undefined 。 返回 Observable 以確定每個組應(yīng)持續(xù)多長時間的函數(shù) 存在。 |
subjectSelector | 可選的。 默認(rèn)值為 undefined 。 類型: () => Subject 。 |
OperatorFunction<T, GroupedObservable<K, R>>
:發(fā)出的可觀察物 GroupedObservables,每個對應(yīng)于唯一鍵值,每個 其中從源Observable發(fā)出共享該密鑰的那些項 值。
When the Observable emits an item, a key is computed for this item with the keySelector function.
If a GroupedObservable
for this key exists, this GroupedObservable
emits. Elsewhere, a new GroupedObservable
for this key is created and emits.
A GroupedObservable
represents values belonging to the same group represented by a common key. The common key is available as the key field of a GroupedObservable
instance.
The elements emitted by GroupedObservable
s are by default the items emitted by the Observable, or elements returned by the elementSelector function.
import { of } from 'rxjs';
import { mergeMap, groupBy, reduce } from 'rxjs/operators';
of(
{id: 1, name: 'JavaScript'},
{id: 2, name: 'Parcel'},
{id: 2, name: 'webpack'},
{id: 1, name: 'TypeScript'},
{id: 3, name: 'TSLint'}
).pipe(
groupBy(p => p.id),
mergeMap((group$) => group$.pipe(reduce((acc, cur) => [...acc, cur], []))),
)
.subscribe(p => console.log(p));
// displays:
// [ { id: 1, name: 'JavaScript'},
// { id: 1, name: 'TypeScript'} ]
//
// [ { id: 2, name: 'Parcel'},
// { id: 2, name: 'webpack'} ]
//
// [ { id: 3, name: 'TSLint'} ]
import { of } from 'rxjs';
import { groupBy, map, mergeMap, reduce } from 'rxjs/operators';
of(
{ id: 1, name: 'JavaScript' },
{ id: 2, name: 'Parcel' },
{ id: 2, name: 'webpack' },
{ id: 1, name: 'TypeScript' },
{ id: 3, name: 'TSLint' }
)
.pipe(
groupBy(p => p.id, p => p.name),
mergeMap(group$ =>
group$.pipe(reduce((acc, cur) => [...acc, cur], [`${group$.key}`]))
),
map(arr => ({ id: parseInt(arr[0], 10), values: arr.slice(1) }))
)
.subscribe(p => console.log(p));
// displays:
// { id: 1, values: [ 'JavaScript', 'TypeScript' ] }
// { id: 2, values: [ 'Parcel', 'webpack' ] }
// { id: 3, values: [ 'TSLint' ] }
groupBy(keySelector: (value: T) => K): OperatorFunction<T, GroupedObservable<K, T>>
Parameters keySelector Type: (value: T) => K
. Returns OperatorFunction<T, GroupedObservable<K, T>>
groupBy(keySelector: (value: T) => K, elementSelector: void, durationSelector: (grouped: GroupedObservable<K, T>) => Observable<any>): OperatorFunction<T, GroupedObservable<K, T>>
Parameters keySelector Type: (value: T) => K
. elementSelector Type: void
. durationSelector Type: (grouped: GroupedObservable) => Observable
. returns OperatorFunction<T, GroupedObservable<K, T>>
groupBy(keySelector: (value: T) => K, elementSelector?: (value: T) => R, durationSelector?: (grouped: GroupedObservable<K, R>) => Observable<any>): OperatorFunction<T, GroupedObservable<K, R>>
參量 | 類型 |
---|---|
keySelector | 類型: (value: T) => K |
elementSelector | 可選的。 默認(rèn)值為 undefined 。類型: (value: T) => R |
durationSelector | 可選的。 默認(rèn)值為 undefined 。 類型: (grouped: GroupedObservable) => Observable |
returns OperatorFunction<T, GroupedObservable<K, R>>
groupBy(keySelector: (value: T) => K, elementSelector?: (value: T) => R, durationSelector?: (grouped: GroupedObservable<K, R>) => Observable<any>, subjectSelector?: () => Subject<R>): OperatorFunction<T, GroupedObservable<K, R>>
參量 | 類型 |
---|---|
keySelector | 類型: (value: T) => K |
elementSelector | 可選的。 默認(rèn)值為 undefined 。 類型: (value: T) => R |
durationSelector | 可選的。 默認(rèn)值為 undefined 。類型: (grouped: GroupedObservable) => Observable |
subjectSelector | 可選的。 默認(rèn)值為 undefined 。類型: () => Subject |
returns OperatorFunction<T, GroupedObservable<K, R>>
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: