Skip to content

Instantly share code, notes, and snippets.

@iohcidnal
Last active May 5, 2018 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iohcidnal/9097d0e22178d923f6cbc3468a4dbb97 to your computer and use it in GitHub Desktop.
Save iohcidnal/9097d0e22178d923f6cbc3468a4dbb97 to your computer and use it in GitHub Desktop.
Updating nested object
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