0

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 ","

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

Isn't it?

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

1 Comment

c: {...foo.c, title: "Cody"} is probably better since c can have multiple properties
0

Will it work?

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

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

console.log(foo2)

1 Comment

c: {...foo.c, title: "Cody"} is probably better since c can have multiple properties

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.