Skip to content

Pushing object in array from defined variable adds them by reference, and they can't be modified without affecting the variable. #7177

@Arwany

Description

@Arwany

Version

2.5.9

Reproduction link

https://jsfiddle.net/arwany/uyndonrp/

Steps to reproduce

After the page loads, the array "persons" will be loaded with one item (Thomas).

Now I have provided two buttons to demonstrate the issue/bug I am having:

  • " Add - OK": this button pushes new object to the array by passing the object in clear js object format.
  • " Add - NOT OK": this button pushes new object to the array by passing an already defined object above (newPerson).

If you click the "Add NOT OK" button two times, and try to change the value of the array in the gray boxes, you will notice that both the object newPerson and the array objects are being changed, as if vue has added them by reference instead of adding them by value (is it even possible?).

What is expected?

To copy the newPerson variable into the array and leave it alone.

Also, when adding multiple objects, I expect to be seperated from each other, but now they seem to be changed all at the same time.

What is actually happening?

I suspect that pushing the object into the array is being added by reference instead of being added by value.


Is this an issue/feature with Javascript or with vue itself?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions