带计数的angular2 rxjs groupby
将angular4与rxjs 5.4.0一起使用 我正在尝试按“类型”对列表进行分组,并获取它们的计数。有人能帮忙吗?下面是我的代码带计数的angular2 rxjs groupby,angular,rxjs,rxjs5,Angular,Rxjs,Rxjs5,将angular4与rxjs 5.4.0一起使用 我正在尝试按“类型”对列表进行分组,并获取它们的计数。有人能帮忙吗?下面是我的代码 export class Sample{ type:string; data:any ... ... } 我有一个示例类数组 list:Sample[] = // number of elements Observable.from(this.list).groupBy(x=> x.type) .flatMap( group =>
export class Sample{
type:string;
data:any ...
...
}
我有一个示例类数组
list:Sample[] = // number of elements
Observable.from(this.list).groupBy(x=> x.type)
.flatMap( group => {
return group.reduce; // how can i use reduce function to count numbers and return map of type and their count
}
})
你很接近了,我想你只需要在分组的可观察对象上再加几个操作符
const list = [{ type: 'foo' }, { type: 'bar' }, { type: 'bar' }];
Observable.from( list ).groupBy( x => x.type )
.mergeMap( list$ => { // each emission is a stream
/* A stream of "aggregated" data. */
const count$ = list$.count();
/* Format the result. */
return count$.map( count => ({ type: list$.key, count }));
});
这将产生:
{ type: 'foo', total: 1 }
{ type: 'bar', total: 2 }
听起来您可能有更复杂的用于计算“聚合”的用例,也许您需要求和Sample.data
。如果是这样,您只需要用自己的实现更改我的count$
实现。假设数据是一个数字列表:
const list = [{
type: 'foo',
data: [1,2,3]
}, {
type: 'bar',
data: [4,5,6]
}, {
type: 'bar',
data: [7,8,9]
}];
Observable.from( list ).groupBy( x => x.type )
.mergeMap( list$ => { // each emission is a stream
/* A stream of "aggregated" data. */
const count$ = list$.reduce( ( accumulator, sample ) => { // reduce the stream
return accumulator + sample.data.reduce( ( acc, datum ) => { // reduce the array
return acc + datum;
}, 0);
}, 0);
/* Format the result. */
return count$.map( count => ({ type: list$.key, count }));
});
这将产生:
{ type: 'foo', total: 6 }
{ type: 'bar', total: 39 }
你很接近了,我想你只需要在分组的可观察对象上再加几个操作符
const list = [{ type: 'foo' }, { type: 'bar' }, { type: 'bar' }];
Observable.from( list ).groupBy( x => x.type )
.mergeMap( list$ => { // each emission is a stream
/* A stream of "aggregated" data. */
const count$ = list$.count();
/* Format the result. */
return count$.map( count => ({ type: list$.key, count }));
});
这将产生:
{ type: 'foo', total: 1 }
{ type: 'bar', total: 2 }
听起来您可能有更复杂的用于计算“聚合”的用例,也许您需要求和Sample.data
。如果是这样,您只需要用自己的实现更改我的count$
实现。假设数据是一个数字列表:
const list = [{
type: 'foo',
data: [1,2,3]
}, {
type: 'bar',
data: [4,5,6]
}, {
type: 'bar',
data: [7,8,9]
}];
Observable.from( list ).groupBy( x => x.type )
.mergeMap( list$ => { // each emission is a stream
/* A stream of "aggregated" data. */
const count$ = list$.reduce( ( accumulator, sample ) => { // reduce the stream
return accumulator + sample.data.reduce( ( acc, datum ) => { // reduce the array
return acc + datum;
}, 0);
}, 0);
/* Format the result. */
return count$.map( count => ({ type: list$.key, count }));
});
这将产生:
{ type: 'foo', total: 6 }
{ type: 'bar', total: 39 }
您可以简化这一点,因为分组所使用的值可以通过上的键
属性获得。也就是说,您不需要使用list$.take(1).map(x=>x.type)
来获取它;只需使用list$.key
。您可以简化此操作,因为分组所使用的值可通过上的key
属性获得。也就是说,您不需要使用list$.take(1).map(x=>x.type)
来获取它;只需使用列出$.key
。