My initial data is:
const menuItems = [{id: 1, active: false}, {id: 2, active: false}]
public menuSubject$ = new BehaviorSubject<MenuItem[]>(menuItems);
public menu$ = this.menuSubject$.asObservable();
I am attempting to update the element with 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 am questioning whether this is the correct method to achieve this using async getValue()
and updating it?