duke2
duke2

Reputation: 13

immutable js modify ALL nested records

Im trying to modify the 'completed' attributes of all Records of the OrderedMap. This is the OrderedMap:

const TodoItem = Record({text: '', completed: false});
let state = OrderedMap({'0': new TodoItem({text: 'First Item'})});

My current code is:

state.forEach((v,k) => {
  state = state.updateIn([k.toString(),'completed'], completed => true);
});
return state;

This works, but how?

Does it work because state is immutable and after the first iteration the forEach runs on the first state obj which is now anonymous?

Are there any other 'better' possibilities to modify attributes of all nested elements?

Upvotes: 1

Views: 125

Answers (1)

hazardous
hazardous

Reputation: 10837

Yes there is -

state = state.map((todoItem)=>todoItem.set("completed", true));

Upvotes: 1

Related Questions