简体   繁体   English

将更新的值传播到对象中会删除未修改的属性

[英]Spreading updated values into an object deletes a property that's not being modified

I'm working on a reducer in Redux, and when trying to update an object by spreading both previous and updated objects into a new one, a property that's not being updated is being erased. 我正在Redux上开发一个reducer,当尝试通过将以前的对象和已更新的对象都散布到一个新对象中来更新对象时,会删除未更新的属性。

Code will explain it better: 代码将更好地解释它:

Previous object: 上一个对象:

previousObj: {
    name: 'Foo',
    category: 'Bar',
    projects: [{...}, {...}]
}

Update object: 更新对象:

updateObj: {
    name: 'Not Foo',
    category: 'Not Bar'
}

Then spreading both into a new one: 然后将两者传播到一个新的:

return {
    ...previousObj,
    ...updateObj
}

The result is the same as updateObj, meaning that the "projects" property, which had a few more objects in it, is gone. 结果与updateObj相同,这意味着其中包含更多对象的“ projects”属性已消失。 In my understanding, when spreading two objects into a new one, the latter properties will overwrite the previous ones if they share the same name, but untouched ones should keep untouched. 以我的理解,当将两个对象散布到一个新对象中时,如果后一个属性共享相同的名称,则后一个属性将覆盖前一个属性,但未更改的属性应保持不变。

Am I missing something here? 我在这里想念什么吗?

Just for the sake of clarity, here's a snapshot of the actual code: 为了清楚起见,下面是实际代码的快照:

实际代码

The log on line 12 has the exact same shape as previousObj, and the one on line 13 has the same shape as updateObj, just actual values were changed for simplicity. 第12行中的日志具有与previousObj完全相同的形状,第13行中的日志具有与updateObj相同的形状,为简单起见,仅更改了实际值。 The result of it is that I can't access "projects" anymore after updating. 结果是更新后我无法再访问“项目”。

Any thoughts will be much appreciated! 任何想法将不胜感激!

I assume your code will be ending something like this: 我认为您的代码将以如下形式结束:

what I can think of is that your spread object is being created correctly, did you try to assign that object to a variable and then return it? 我能想到的是,正确创建了您的spread object ,您是否尝试过将该对象分配给变量然后返回? the purpose of this is that you check that the correct key/values are being created. 目的是要检查是否创建了正确的键/值。

 const previousObj = { name: 'Foo', category: 'Bar', projects: [{ t: 'test1' }, { t: 'test2' }] } const updatedObj = { name: 'not Foo', category: 'not Bar' } const newObj = { ...previousObj, ...updatedObj } console.log(newObj) 

Also I can assume that your updatedObj is corrupted and therefore you have a problem with your spread object, check that updatedObj is not assigning something to the projects key, as the example below. 另外,我可以假设您的updatedObj已损坏,因此您的传播对象有问题,请检查updatedObj没有为projects密钥分配某些内容,如下例所示。

 const previousObj = { name: 'Foo', category: 'Bar', projects: [{ t: 'test1' }, { t: 'test2' }] } const updatedObj = { name: 'not Foo', category: 'not Bar', projects: undefined } const newObj = { ...previousObj, ...updatedObj } console.log(newObj) 

and the last would be that somewhere after you are returning the spread object (which will be completed) you are deleting or overriding the key. 最后一个是在您返回散布对象(将完成)之后的某个位置,您正在删除或覆盖键。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM