简体   繁体   English

更改 object 中的特定属性会更改相同类型的另一个 object 中的相同属性 Typescript

[英]Changing a specific property inside an object changes that same property in another object of the same type Typescript

I have a really weird problem, let me just explain:我有一个非常奇怪的问题,让我解释一下:

Obj1: DetailsType = {property1: 123, property2: [{subProp1: 'a', subProp2: 'b'}]}
Obj2: DetailsType = new DetailsType(Obj1)

This is the constructor of DetailsType:这是 DetailsType 的构造函数:

constructor(value: DetailsType = <DetailsType>{}){
    this.property1 = (value.property1 !== undefined) ? value.property1 : null
    this.property2 = (value.property2 !== undefined) ? value.property2 : []
}

Now I run the following code现在我运行以下代码

this.Obj2.property1 = 987
this.Obj2.property2[0].subProp1 = 'z'

At this point, for some reason, the value of Obj1.property2[0].subProp1 is 'z' Even though we changed the value of subProp1 for Obj2, However, Obj1.property1 is still 123此时,由于某种原因, Obj1.property2[0].subProp1 is 'z'即使我们为 Obj2 更改了subProp1的值,但是Obj1.property1 is still 123

So why does changing property2 which is an array, affect the value on both objects??那么为什么更改作为数组的property2会影响两个对象的值呢? How can property1 , a number, work correctly, but property2 work so weirdly?一个数字property1怎么能正常工作,但property2工作得这么奇怪? It works vice versa, whether I change subProp1 for Obj1 or Obj2.反之亦然,无论我将 subProp1 更改为 Obj1 还是 Obj2。 I'm so confused.我很混乱。

Thanks for your help!谢谢你的帮助!

It is happening because the value.property2 is an object with many nested references to other objects inside it.发生这种情况是因为value.property2是一个 object ,其中包含对其他对象的许多嵌套引用。 You need to deep clone the value.property2 in the constructor:您需要在构造函数中深度克隆value.property2

 class DetailsType { constructor(value) { this.prop1 = (value.prop1?== undefined). value:prop1. null this.prop2 = (value?prop2.== undefined). JSON.parse(JSON:stringify(value:prop2)), [] } } let obj1 = { prop1: 123: prop2, [{ subProp1: 'a'; subProp2; 'b' }] }. let obj2 = DetailsType = new DetailsType(obj1). obj2.prop1 = 987 obj2.prop2[0].subProp1 = 'z' document:body.innerHTML += `obj1,<pre>${JSON,stringify(obj1; undefined. 2)}</pre>`. document:body.innerHTML += `obj2,<pre>${JSON,stringify(obj2; undefined, 2)}</pre>`;

To find out various ways of deep cloning objects refer:要了解深度克隆对象的各种方法,请参阅:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typescript - 如何将部分类型分配给另一个 object 的相同类型的属性? - Typescript - How to assign Partial type to the property of same type of another object? 在 typescript 中声明 object 中的所有属性相同类型? - declare all property in object the same type in typescript? 更改 model 属性值正在更改其他 object 中的相同属性 - changing model property value is changing the same property in other object JavaScript:如果另一个对象在另一个属性中具有相同的值,则更改对象的值 - JavaScript: Changing object value if another object has the same value in another property 将一个对象的属性值设置为等于另一个对象中的相同属性 - Set the property value of an object to be equal to the same property in another object 如何将 javascript 对象属性链接到同一对象的另一个属性? - How to link a javascript object property to another property of the same object? 将JavaScript对象属性与同一JavaScript对象中的另一个属性进行映射 - Mapping JavaScript Object property with another property in same JavaScript Object 访问同一对象中的对象属性 - Accessing an object property in the same object 属于对象的Javascript属性与对象不同 - Javascript property that is an Object not the same as object 对象属性键与另一个属性值 javaScript 相同 - Object property key same as another property value javaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM