Last active
May 5, 2018 21:08
-
-
Save iohcidnal/9097d0e22178d923f6cbc3468a4dbb97 to your computer and use it in GitHub Desktop.
Updating nested object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const a = { | |
id: 1, | |
name: { | |
lastName: '', | |
} | |
}; | |
const n = 'name'; | |
const b = { | |
...a, | |
[n]: { | |
...a[n], | |
lastName: 'bbb' | |
} | |
}; | |
console.log(b); | |
console.log(a); | |
// using setState | |
state = { | |
user: { | |
id: 1, | |
name: { | |
firstName: '', | |
lastName: '', | |
} | |
} | |
}; | |
this.setState({ | |
user: { | |
...this.state.user, | |
name: { | |
...this.state.user.name, | |
firstName: 'John', | |
lastName: 'Doe', | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment