[Vuejs]-Copy object and change nested property in Vue

2👍

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.

2👍

foo2: {...foo, c: {...foo.c, title: 'Copy'}},

0👍

Isn’t it?

const foo2 = { ...foo, c: { title: "Copy" } }
👤iopzhu

0👍

Will it work?

const foo = {a: 'A', b: 'B', c: {title: "Main"}}

const foo2 = { ...foo, c: { ...foo.c,  title: 'Copy' } }

console.log(foo2)

Leave a comment