I have the following code:
todos$ = new BehaviorSubject<TodoInterface[]>([]);
filter$ = new BehaviorSubject<FilterEnum>(FilterEnum.all);
addTodo(text: string): void {
const newTodo: TodoInterface = {
id: Math.random().toString(16),
text: text,
isCompleted: false,
};
const updateTodos = [...this.todos$.getValue(), newTodo];
this.todos$.next(updateTodos);
console.log(this.todos$);
}
toggleAll(isCompleted: boolean): void {
const updatedTodos = this.todos$.getValue().map((todo) => {
// todo.isCompleted = isCompleted;
return {
...todo,
isCompleted,
};
});
this.todos$.next(updatedTodos);
}
The thing I achieve with the toggleAll function is that every Todo object's isCompleted property will change from false to true. I follow along a tutorial and there the instructor changed the property's value the way seen above, however, it is totally not clear for me how after the spread operator the boolean value of the argument 'isCompleted' can overwrite the object property's value. Could someone please explain it?
The commented part is what I personally wanted to do without using the next method and I think it's a better approach and less confusing.