Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get one object from observable array

How do you write a function that takes an Observable<T[]> and returns Observable<T>?

For example, I have two methods:

getTransactions(): Observable<Transaction[]>{
        ...
    }
getTransaction(id: number): Observable<Transaction>{
    return this.getTransactions().pipe(find(txn => txn.id === id));
}

I get the error that type 'Observable' is not assignable to type 'Observable'. Type 'Transaction[] is not assignable to type 'Transaction'. Property 'id' is missing in type 'Transaction[]'.

As I understand, the observable pipe functions (map, single, find, max, etc.) pertain to a stream of data (i.e., while the observable is emitting multiple items over time) and are not useful when the observable emits a single item (which happens to be an array) at once.

What is the proper way to write the getTransaction(id) function? I tried:

let transactions : Transaction[] = [];
this.getTransactions().subscribe(txns => transactions=txns);
return of(transactions.find(txn => txn.id === id));

But this causes an error saying 'transaction' is undefined. I'm very new to Angular etc. so any instruction is helpful. Thank you!

Stackblitz

I'm using:
Angular CLI: 6.0.3
Angular: 6.0.1
rxjs: 6.1.0

like image 244
BobtheMagicMoose Avatar asked May 21 '18 17:05

BobtheMagicMoose


People also ask

Can you filter an Observable array?

Observable (Angular) & Solution Yes, it's really that simple. Pipe the item array in the itemObservable, map each item in the array and check whether or not it matches the condition. The condition in our case is to check whether or not it matches the search query.

How do you remove an element from an Observable array?

pop() — Removes the last value from the array and returns it. unshift( value ) — Inserts a new item at the beginning of the array. shift() — Removes the first value from the array and returns it. reverse() — Reverses the order of the array and returns the observableArray (not the underlying array).

What is pipe operator in RXJS?

A Pipeable Operator is a function that takes an Observable as its input and returns another Observable. It is a pure operation: the previous Observable stays unmodified. A Pipeable Operator is essentially a pure function which takes one Observable as input and generates another Observable as output.


1 Answers

You need to be treating the observable value as though it is a stream of arrays, rather than attempting to treat it like a stream of streams of items. The values in your observable are single points of data that each contain their own array.

getTransaction(id: number): Observable<Transaction>{
    return this.getTransactions().pipe(
        map(txs => txs.find(txn => txn.id === id))
    );
}

What you're doing here is mapping each array in your stream into whatever item has an ID that matches the parameter.

like image 170
bryan60 Avatar answered Sep 17 '22 17:09

bryan60