I have this kind of object in data:
data () {
return {
foo: {a: 'A', b: 'B', c: {title: "Main"}};
}}
I need to get exact copy of this object but change one thing there
foo2: {a: 'A', b: 'B', c: {title: "Copy"}};
I tried setting foo2
in data as:
foo2: {...foo, c['title']:'Copy'},
But Im getting error:
error Parsing error: Unexpected token, expected ","
foo2: {...foo, c: {...foo.c, title: 'Copy'}},
Spread foo
into its own object with a c
property with a value of an object. Then spread foo.c
into this object, which you can add the title
property to like so:
foo2: {...foo, c: {...foo.c, 'title':'Copy'}
Spreading foo.c
will allow any additional own enumerable properties (if any) from foo.c
to remain in the new c
object value.
Isn't it?
const foo2 = { ...foo, c: { title: "Copy" } }
Will it work?
const foo = {a: 'A', b: 'B', c: {title: "Main"}} const foo2 = {...foo, c: {...foo.c, title: 'Copy' } } console.log(foo2)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.