functional programming has introduced immutable, so es6's map is quite a popular approach when working with data, specifically in arrays and object.
I have a reducer of redux below
const todos = (state = [], action) => {
switch(action.type) {
case 'TOGGLE_TODO': {
return state.map(todo =>
todo.id !== action.id ? todo
: {
...todo, checked: !todo.checked
}
)
}
default:
return state
}
}
What's the alternative to change the checked property besides above approach? Is the code even readable at all?
someone prefer this
switch (action.type) {
case "TOGGLE_TODO":
return [
...state.slice(0, action.id),
{...state[action.id], isCompleted: !state[action.id].isCompleted},
...state.slice(action.id + 1)
]
default:
return state;
}