How to change value in object in array BehaviorSubject?

Viewed 1196

I have a initial data as: const menuItems = [{id: 1, active: false}, {id: 2, active: false}]

  public menuSubject$ = new BehaviorSubject<MenuItem[]>(menuItems);
  public menu$ = this.menuSubject$.asObservable();

I try to modify element where id: 1:

modify(id: number) {
   const menuItemsUpdated = this.menuSubject$.getValue().map((item) => {
      if(item.id === id) {
         return {...item, active: true};
      }
      return item;
   });

    this.menuSubject$.next(menuItemsUpdated);
}

I wonder is it properly way to do that using async getValue() and pushing this back?

1 Answers

Seems ok. I would do the same thing .

Related