I am attempting to update a single property of a nested state object and return it to the state. I have the index of the object that I need to update but I am struggling to change one property and add the new object back to the state.
My state object looks like this and I am trying to reduce the lives of a player on a button press.
I have been playing around with using array filter, trying to copy the object and add it back into the state but this then loses the objects position.
this.state = {
players: {
{id: 1, lives: 3, name: something},
{id: 2, lives: 3, name: something}
}
}
public removeLife(id): void {
const player = this.state.players[id];
player.lives = player.lives - 1;
this.setState({players[id]: {...player}
})
}
Any help would be greatly appreciated.