How to remove specific element from Observable>

前端 未结 4 1092
你的背包
你的背包 2020-12-15 20:22

There is an Observable of the array of places:

places: Observable>;

In template it used with the async pipe:

4条回答
  •  囚心锁ツ
    2020-12-15 20:42

    RxJS version 6

    Using the accepted answer with RxJS 6 and typescript will throw an error because the observables hold different types. you better use combineLatest, you could also use zip but it will not work! did you just ask why? the answer is here :)

    combineLatest([
      this.items$,
      this.deleteItem$
    ]).pipe(
      takeUntil(this.onDestroy),
      tap(([items, deleteItem]) => {
        if (deleteItem && deleteItem.op === 'deleteItem') {
          var index = items.findIndex((item) => item.id === deleteItem.id);
          if (index >= 0) {
            items.splice(index, 1);
          }
          return items;
        }
        else {
          return items.concat(deleteItem);
        }
      })
    ).subscribe();
    

    then you can send the event..

    this.deleteItem$.next({ op: 'deleteItem', id: '5e88fce485905976daa27b8b' });
    

    I hope it will help someone..

提交回复
热议问题