disclaimer: I have a creative solution and would like to share it here.
While exploring the RxJS documentation for map methods that meet this specific requirement - let's call it mapArray
- I haven't been able to find one.
of([1, 2, 3]).pipe(
mapArray(num => num + num)
).subscribe(value => console.error(value));
I am seeking an operator that can transform an Observable with one or more arrays (Obserable<T[][]>
) and apply a mapping operation on each individual element within those arrays.
A common scenario for me involves fetching a list of elements from a server and converting each JSON representation into their respective TypeScript classes. This typically involves working with a single item observable (just one array), although I am interested in the broader case (1 or more arrays).
let people$: Observable<Person> = of([{id: 1, name: 'joe', timestamp: '2019-10-11'}, {...}, ...]).pipe(
mapArray(Person.fromJson)
);
I've encountered this issue frequently enough to consider creating a custom operator for it. Does anyone know if there is a built-in RxJS operator that can handle this?