rxjs - Angular 2 filter an Observabble<object[]> -
i want show list of imessage on screen , able filter them using pipe. message observable<imessage[]> on want filter each imessage checking isprivate property. code of messagestatuspipe looks this:
export class messagestatuspipe { transform(message: observable<imessage[]>, privatefilter: bool) { //here want return messages pass privatefilter, how? } } i have read questions seem similar don't seem able apply solution. following solution luka jacobowitz seems need. https://stackoverflow.com/questions/37991713/simple-filter-on-array-of-rxjs-observable#=
============ update answer =============
as meir pointed out pipe returned bool value instead of sub array containing items matched filter. working pipe looks this:
export class messagestatuspipe { transform(messages: observable<imessage[]>, privatefilter: bool) { return messages.map(m => m.filter((message, i) => { return message.isprivate == privatefilter; })); } }
your action can (should) broken 2 parts: 1. getting async values 2. filtering them
so, can change pipe's signature to:
transform(messages: imessage[], privatefilter: bool) { ... } and in component do:
<div *ngfor="let msg of messages | async | messagestatuspipe">...</div> if reason want combine them 1 pipe, can have @ article wrote shows how handle async values inside pipe , adapt needs (mainly, add filtering loging)
============ added filter logic ==============
return messages.filter(privatefilter);
Comments
Post a Comment