0

let pppp = {
    name: "duanxiao",
    age: 1,
    job: {
        title: "~~~"
    }
};
let ppppCopy = {};

({
    name: ppppCopy.name,
    age: ppppCopy.age,
    job: ppppCopy.job
} = pppp);

pppp.job.title = "Hacker";

console.log(pppp);
console.log(ppppCopy);

The output values ​​are the same.

Why modifying the value of one object, the other object will also be modified?

Whenever I modify the value of one object, the value of the other object is also modified.

4
  • Can't reproduce that. ppppCopy doesn't contain anything in the jbo property Commented Nov 16, 2022 at 13:41
  • sorry should be job, not jbo Commented Nov 16, 2022 at 13:42
  • 1
    This is normal as you are not copying the value from that object but a reference to that key in the object. Commented Nov 16, 2022 at 13:44
  • 1
    The job object will be passed by reference. Therefore it will affect both the original and "copy" object. Commented Nov 16, 2022 at 13:45

4 Answers 4

2

Because you pppp and ppppCopy holds the same reference of job property. Changing at one location will impact another. You can achieve your intended outcome with below code using ES6 spread operator,

let pppp = {
    name: "duanxiao",
    age: 1,
    job: {
        title: "~~~"
    }
};

const ppppCopy = {
  ...pppp,
  job: { ...pppp.job },
};

With this, updating pppp.job.title will not impact ppppCopy.job.title.

You can also use the traditional way like JSON.parse(JSON.stringify(pppp)), but you need to be more cautious while using this approach as it strips down the function property

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

1 Comment

yes,it is a shallow copy. Thank you
2

In JS, and many other languages, you have data types that store by value, like Number and other primitive types. Some data types stored by reference, like Arrays, Object.

By destructing pppp you just passing the reference to the inner job object, and not duplicating it, so technically its the same job object in pppp and ppppCopy.

Here I added a manipulation to a primitive, and you can see that there is a difference.

let pppp = {
    name: "duanxiao",
    age: 1,
    job: {
        title: "~~~"
    }
};
let ppppCopy = {};

({
    name: ppppCopy.name,
    age: ppppCopy.age,
    job: ppppCopy.job
} = pppp);

pppp.job.title = "Hacker";
pppp.age = 123;

console.log(pppp);
console.log(ppppCopy);

Here is another good answer related

1 Comment

Thanks, that is, this is a shallow copy right? 👍
0

Since objects are non-primitive data types javascript makes a reference of the original object when you make a copy using the assignment operator as you have done in your case. In order to avoid shallow copying you can use the spread operator i.e. let copy = { ...original} or you can use the assign method i.e. let copy = Object.assign({}, original) but both of these fail when you have a nested object. In order to deep copy a nested object you need to do it as Edoardo pointed out above but that will fail too when there is a function in your object. Ravindra's method can also be used, but it will be a hassle when you have multiple nested objects.

The best way to do it in my opinion is to use lodash _.cloneDeep() method. You can read more about it here

1 Comment

Thanks! Your answer is very detailed.I am a beginner and I don't understand it deeply.
-1

The only way is to use JSON.parse(JSON.stringify(pppp));

    name: "duanxiao",
    age: 1,
    job: {
        title: "~~~"
    }
};
let ppppCopy = JSON.parse(JSON.stringify(pppp));



pppp.job.title = "Hacker";

console.log(pppp);
console.log(ppppCopy);

4 Comments

Why the only way?
Please add some explanation to your answer such that others can learn from it. "The only way" to achieve what?
What I mean is why one object is modified, and the other object will change. Because it is a shallow copy, right?
Feel free to add all clarification to your answer by editing it

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.