arraysangularrxjsobservableangular-filters

observables, arrays and filtering


using angular 8, rxjs 6.5.2

I have an http call which returns an observable of

{
  'A': {name: 'A', type:'1'},
  'B': {name: 'B', type:'2'},
  'C': {name: 'C', type:'2'}
}

I store this call in data$

I want to show all type "2" in the angular form. I know that to convert an object into an array, I can use Object.values(data)

so how do I convert the observable into an array which can then be filtered and changed back into an observable ?

I have tried this code

    this.myService.getPeople().subscribe(data => {
        const items = Object.values(data).filter(
            (item: any) => item && item.type === '2'
        );

        this.data$ = of(items);
    });

but the form never updates or refreshes

Am I missing something obvious ?


Solution

  • You will need to use map operator.

    this.data$ = this.myService.getPeople().pipe(map(data => {
                const items = Object.values(data).filter(
                    (item: any) => item && item.type === '2'
                );
                  return items;
            });