2

I have 2 variable masterdata and datatoedit. masterdata have array value and datatoedit have object value.

I want to copy data 1 from masterdata to variable datatoedit, so I write like this:

this.datatoedit = this.masterdata[0]

But when I edit data from datatoedit on form data on masterdata[0] also changed.

How to prevent masterdata[0] from changed when I edit datatoedit?

1

3 Answers 3

3

You can use ES6 Spread syntax to make an object copy:

this.datatoedit = {...this.masterdata[0]}
Sign up to request clarification or add additional context in comments.

3 Comments

is this work on vuex state too?, you know some time im copy a state from vuex too..
The result is the same, if you modify the copied object, the original object will not be updated.
This can go wrong in case of deeply nested objects.
2

The accepted answer can go wrong if the masterdata elements have nested objects like

{
   name: 'John',
   age: 30,
   address: {
       street: 'ChurchStreet',
       city: 'Bangalore',
       state: 'KA'
   }
}

    var masterdata = [
        {
            name: 'John',
            age: 30,
            address: {
                street: 'ChurchStreet',
                city: 'Bangalore',
                state: 'KA'
            }
        },
        {
            name: 'Jane',
            age: 26,
            address: {
                street: 'West of Chord',
                city: 'Mumbai',
                state: 'MH'
            }
        }
    ];

    // With spread operator
    var datatoedit = {...masterdata[0]};
    datatoedit.age = 32;
    datatoedit.address.street = 'Infantry Road';

    console.log('With spread operator');
    console.log('masterdata[0]: ' + JSON.stringify(masterdata[0]));
    console.log('datatoedit: ' + JSON.stringify(datatoedit));


    // With Object.assign()
    datatoedit = Object.assign({}, masterdata[0]);
    datatoedit.age = 35;
    datatoedit.address.street = 'Brigade Road';
    console.log('With Object.assign()');
    console.log('masterdata[0]: ' + JSON.stringify(masterdata[0]));
    console.log('datatoedit: ' + JSON.stringify(datatoedit));


    // Now with JSON.parse() and JSON.stringify()
    datatoedit = JSON.parse(JSON.stringify(masterdata[0]));
    datatoedit.age = 35;
    datatoedit.address.street = 'West of Chord Road';

    console.log('With JSON.parse() and JSON.stringify()');
    console.log('masterdata[0]: ' + JSON.stringify(masterdata[0]));
    console.log('datatoedit: ' + JSON.stringify(datatoedit));

 

As you can see, in case of nested objects, both spread operator and Object.assign() can go wrong as they do shallow copy.

Combining JSON.parse() and JSON stringify() creates the effect of deep copy and hence it works fine all cases. (Though these functions are not meant for deep copy as such).

Comments

2

You can also do this using lodash function clonedeep. It will not copy the references.

data_to_edit = _.cloneDeep(masterdata[0])

You can now modify data_to_edit as your wish.

Comments

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.