Tag Archives: rxjs

Filter an array of Observable Object using RxJS

map((reports : IReport[]) => reports.filter(p => p.reportName === name))
findItemsByParentId(id: number): Promise<Item[]> {
  return new Promise<Item[]>((resolve, reject) => {
    this.localStorage.getItem('items')
      .pipe(map((items: Item[]) => items.filter((item: Item) => item.parentId === id)))
      .subscribe((value: Item[]) => {
        resolve(value);
      });
  });
}

References
https://www.learnrxjs.io/operators/transformation/map.html
https://rxjs.dev/api/operators/map
https://stackoverflow.com/questions/52263374/angular-rxjs-observable-filter-on-array-of-objects

Filter an Observable using RxJS

filter objects based on property

// RxJS v6+
import { from } from 'rxjs';
import { filter } from 'rxjs/operators';

//emit ({name: 'Joe', age: 31}, {name: 'Bob', age:25})
const source = from([{ name: 'Joe', age: 31 }, { name: 'Bob', age: 25 }]);
//filter out people with age under 30
const example = source.pipe(filter(person => person.age >= 30));
//output: "Over 30: Joe"
const subscribe = example.subscribe(val => console.log(`Over 30: ${val.name}`));

References
https://www.learnrxjs.io/operators/filtering/filter.html
https://rxjs.dev/api/operators/filter