'New object created with the help of spread operator behaving like it is a deep copy, even though it has a nested object
So i created a new object from one object using spread operator and used another object to modify is property values.
But i am not sure, why it behaves as it is a deep copy instead of a shallow copy.
Cause as much as i remember, the way that i have done it, it should create shallow copy only in javascript (Node 12 or 14 version).
const modifier = {
policyName: "Asa1",
test: {
a: "As1",
bc : [
{
a: 1
}
]
}
}
const memberDetails = {
policyName: 'policyName',
test: {
a: "As",
bc : [
{
a: 0
}
]
}
};
const generatedMemberDetails = {...memberDetails, ...modifier};
generatedMemberDetails.test.a = "newAs";
generatedMemberDetails.test.bc[0].a = "new0";
console.log(JSON.stringify(memberDetails) );
console.log(JSON.stringify(generatedMemberDetails));
Output:
{"policyName":"policyName","test":{"a":"As","bc":[{"a":0}]}}
{"policyName":"Asa1","test":{"a":"newAs","bc":[{"a":"new0"}]}}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|