Last active
June 9, 2017 06:27
-
-
Save SeanPlusPlus/4dd815bb5d96835e2bdf009a5c141a70 to your computer and use it in GitHub Desktop.
update nested property using Object.assign
This file contains hidden or 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 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