0

I am searching a solution where I can replace a object content with other in a array of objects.

The thing is, I don't know what my function will pass, so I can't pass the values inside as keys directly, so the reference won't be copied, is there any way I can assign the value directly without passing the reference? I know that objects pass references and not values, but is there a way to do that?

I tried two ways:

state.document["atributes"].splice(state.document["atributes"][state.currentIndex],1,section);

And

state.document["atributes"][state.currentIndex] = section

Where my state.document["atributs"] is my array, and the state.currentIndex the index where I want to replace the element inside my array.

What happens at the moment is that my object can be a Table a paragraph etc.

If the objects are the same it replaces the content :/

Any help with this? Thank you

Fotis Grigorakis
  • 363
  • 1
  • 3
  • 16
Filipe Costa
  • 545
  • 3
  • 12
  • 32
  • Possible duplicate of [What is the most efficient way to deep clone an object in JavaScript?](https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript) – Shubham Patel Aug 21 '17 at 10:00
  • your second solution should be allright enough, but instead of assigning the object as a value, you should assign a deep copy of it (see other comments links on how to make a deep copy) – Kaddath Aug 21 '17 at 10:01

1 Answers1

0

If you're only modify what in the currentIndex, try

Vue.set(state.document["atributes"], state.currentIndex, section)

Reference: Vue documentation

tony19
  • 125,647
  • 18
  • 229
  • 307
kevguy
  • 4,328
  • 1
  • 24
  • 45