简体   繁体   English

JS检查对象中是否存在对象并将值添加到格式化对象中

[英]JS check if object exists in object and add the value to the formatted object

I am filling in fields for a form and I have a formattedData object which keeps a deep copy of the original data object.我正在填写表单的字段,并且我有一个 formattedData 对象,它保留原始数据对象的深层副本。 In the form if a field changes, I need to change the formatted data object with properties in the values object.在表单中,如果字段更改,我需要使用值对象中的属性更改格式化数据对象。

I have made a mock data object of the fields:我制作了字段的模拟数据对象:

const formattedData = {
            model_type: 'Config',
            accessModel: '5732132',
            // Random fields
            advancedData: {
              isLion: {
                enabled: true,
                animalType: 'Cat',
                move: {
                  model_type: '',
                  source: '',
                  value: 'auto'
                }
              },
              isTiger: {
                enabled: true,
                animalType: 'Cat',
                move: {
                  model_type: '',
                  source: '',
                  value: 'auto'
                }
              }
            },
            data: {
              isLion: {
                enabled: true,
                animalType: 'Cat',
                id: 8,
                power: 6
              },
              isTiger: {
                enabled: true,
                animalType: 'Cat',
                id: 8,
                power: 6
              }
            }
          }

This is the object from my form:这是我表单中的对象:

const values = {
            advancedData: {
              isLion: {
                move: {
                  value: '89'
                }
              },
              isTiger: {
                move: {
                  value: '89'
                }
              }
            }
          }

I need to check if values.advancedData : isLion or isTiger has changed then, put that changed value into the formattedData object.我需要检查,如果values.advancedDataisLionisTiger已经改变,那么,把那个改变值到formattedData对象。 In my example I gave, the user changed values.advancedData.isLion and values.advancedData.isTiger and I need to put those values into formattedData without changing anything other than the value property.在我给出的示例中,用户更改了values.advancedData.isLionvalues.advancedData.isTiger ,我需要将这些值放入formattedData而不更改 value 属性以外的任何内容。

Also, if the value has changed, I need to change the source to 'manual'此外,如果值已更改,我需要将source更改为“手动”

You can grab the new value from values and then assign it to formattedData :您可以从values获取新values ,然后将其分配给formattedData

// choose your favorite animal
const isAnimal = 'isLion'

// Grab the value from `values`
const new_value = values.advancedData[isAnimal].move.value

// Put the new value info `formattedData`
formattedData.advancedData[isAnimal].move.value = new_value

A simple way to achieve this is as shown below, assuming the data structure will always be the same.实现此目的的简单方法如下所示,假设数据结构始终相同。

// DATA
// For brevity I omitted the data body, but same as yours

const formattedData = {
    // ... rest of data body
};

const values = {
   // ... rest of data body
};


// SOLUTION - Using object spread

formattedData.advancedData.isLion = { ...formattedData.advancedData.isLion, ...values.advancedData.isLion };
formattedData.advancedData.isLion.move.model_type = 'manual';

formattedData.advancedData.isTiger = { ...formattedData.advancedData.isTiger, ...values.advancedData.isTiger };
formattedData.advancedData.isTiger.move.model_type = 'manual';

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

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