Here it says :
// RxJS v6+
import { from } from 'rxjs';
import { groupBy, mergeMap, toArray } from 'rxjs/operators';
const people = [
{ name: 'Sue', age: 25 },
{ name: 'Joe', age: 30 },
{ name: 'Frank', age: 25 },
{ name: 'Sarah', age: 35 }
];
//emit each person
const source = from(people);
//group by age
const example = source.pipe(
groupBy(person => person.age),
// return each item in group as array
mergeMap(group => group.pipe(toArray()))
);
/*
output:
[{age: 25, name: "Sue"},{age: 25, name: "Frank"}]
[{age: 30, name: "Joe"}]
[{age: 35, name: "Sarah"}]
*/
const subscribe = example.subscribe(val => console.log(val));
In my code I don't create an observable with the 'from' operator, but rather with BehaviorSubject.asObservable() method.
Person { name: string, age: number }
private _all: BehaviorSubject<Person[]>;
all: Observable<Person[]>;
constructor() {
this._all = new BehaviorSubject<Person[]>([]);
this.all = this._all.asObservable();
}
I can iterate over 'all' with the async pipe. But when I try to use the groupBy operator I get the array itself, instead of the containing persons one by one as a stream :
this.all.pipe(
groupBy(
item => ... <-- here 'item' is Person[], not a Person
)
);
What am I doing wrong ?