0

I have the following object:

{ property1:'value1', property2:'value2', property3:[{ property4:'value4' },{ property5: 'value5'}], property6:'value6' }

And now I want to push the following into the value of property3

{ property7:‘value7’}

The final result would be like

{ property1:'value1', property2:'value2', property3:[{ property4:'value4' },{ property5: 'value5'},{property7:'value7'}], property6:'value6'}

Any idea how to do this?

I tried Object.assignbut that does not work in this case because of the array.

1
  • 2
    Why not just obj.property3.push({ property7: 'value7'})? Commented Dec 5, 2019 at 21:18

1 Answer 1

1

try this

obj.property3.push({ property7: 'value7'})
obj={...obj}
Sign up to request clarification or add additional context in comments.

4 Comments

Why should he do that? What does that do? How does it answer the question? How To Answer
Adding property to an object is straight forward. But when you make this multi level object reactivity may get loss. What I suggested is assigning new object to the variable with old value which forces it to rerender
@Shirish looking at the console log your suggestion adds the newly added data correctly into the object. But reactivity seems to be lost. there is no re-rendering.
There was an error in my code. I got it working now. Thanks!

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.