Skip to content

Instantly share code, notes, and snippets.

@SeanPlusPlus
Last active June 9, 2017 06:27
Show Gist options
  • Save SeanPlusPlus/4dd815bb5d96835e2bdf009a5c141a70 to your computer and use it in GitHub Desktop.
Save SeanPlusPlus/4dd815bb5d96835e2bdf009a5c141a70 to your computer and use it in GitHub Desktop.
update nested property using Object.assign
const state = {
meta: {
attrs: [ 'dude' ],
data: {
foo: 'bar',
stuff: [
'x',
'y',
]
},
},
}
console.log(JSON.stringify(state, null, 2))
const hello = { hello: 'world' }
const data = Object.assign(
{},
state.meta.data,
hello,
)
const meta = Object.assign(
{},
state.meta,
{ data },
)
const state2 = Object.assign(
{},
state,
{ meta },
)
console.log(JSON.stringify(state2, null, 2))
// Transforms this:
// {
// "meta": {
// "attrs": [
// "dude"
// ],
// "data": {
// "foo": "bar",
// "stuff": [
// "x",
// "y"
// ]
// }
// }
// }
// To this:
// {
// "meta": {
// "attrs": [
// "dude"
// ],
// "data": {
// "foo": "bar",
// "stuff": [
// "x",
// "y"
// ],
// "hello": "world"
// }
// }
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment