首页 > 解决方案 > 更改对象的更深层属性会显示在控制台中,但不会在我们打开它或稍后访问它时显示

问题描述

更改对象的更深层属性会显示在控制台中,但不会在我们稍后打开或访问它时显示。以下是我的对象:

 section@shikhar:
1: {displayName: "Entity", dropdownType: "SINGLESELECT", fieldType: "DROPDOWN", values: Array(24), template: null, …}
2:
customTooltip: null
defaultValue: null
dependentValues: {AVS - Avionics: Array(4), DMS - Defence Mission Systems: Array(4), LAS - Land and Air Systems: Array(5), SIX - Secure Communications & Information Systems: Array(4), GTS - Ground Transportation Systems: Array(4), …}
displayName: "Business Line"
dropdownType: "SINGLESELECT"
fieldType: "DROPDOWN"
isDashboard: true
isOptional: false
isParentOf: "3"
isReporting: true
isTooltipEnabled: null
parent: 1
parentValue: ["depend"]
productTooltip: null
sequenceNumber: 2
template: null
values: []
__proto__: Object
3: {displayName: "Product Line Family", dropdownType: "SINGLESELECT", fieldType: "DROPDOWN", values: Array(0), template: null, …}
4: {displayName: "Product Line", dropdownType: "SINGLESELECT", fieldType: "DROPDOWN", values: Array(0), template: null, …}
5: {displayName: "Brief Instructions", dropdownType: null, fieldType: "TEXT", values: null, template: null, …}
6: {displayName: "Main message", dropdownType: null, fieldType: "TEXT", values: null, template: null, …}
7: {displayName: "Tone of voice", dropdownType: "MULTISELECT", fieldType: "DROPDOWN", values: Array(10), template: null, …}
8: {displayName: "Audience to address (Primary)", dropdownType: "MULTISELECT", fieldType: "DROPDOWN", values: Array(2), template: null, …}
9: {displayName: "Target to reach (Secondary)", dropdownType: "MULTISELECT", fieldType: "DROPDOWN", values: Array(9), template: null, …}
10: {displayName: "Sub-type", dropdownType: "SINGLESELECT", fieldType: "DROPDOWN", values: Array(2), template: null, …}
11: {displayName: "Size", dropdownType: "SINGLESELECT", fieldType: "DROPDOWN", values: Array(0), template: null, …}
12: {displayName: "Custom Size", dropdownType: null, fieldType: "TEXT", values: Array(0), template: null, …}
13: {displayName: "Copyrights: Expiry Date", dropdownType: null, fieldType: "DATE", values: null, template: null, …}
14: {displayName: "Output Format", dropdownType: "SINGLESELECT", fieldType: "DROPDOWN", values: Array(4), template: null, …}
15: {displayName: "Other", dropdownType: null, fieldType: "TEXT", values: Array(0), template: null, …}
16: {displayName: "Tags", dropdownType: null, fieldType: "TEXT", values: null, template: null, …}

这里的问题是,即使我在“2”对象中设置值,它仍然显示为 [] 但实际上它应该有 4 个值,例如:

{displayName: "Business Line", dropdownType: "SINGLESELECT", fieldType: "DROPDOWN", values: Array(4), template: null, …

我知道该对象正在以某种方式发生变异,因为即使控制台也说该值刚刚被评估。

我正在使用的代码是:

 updateAllCommonValues=(val,id)=>{
    const { commonTemplateFields } = this.state;

    var commonTemplateValues = JSON.parse(JSON.stringify(commonTemplateFields));
    _.forEach(commonTemplateValues,(section,k)=>{
        _.forEach(section,(field,key)=>{
             if(key == id){
            field.value = val
                 if(field.isParentOf != null){
                    commonTemplateValues[k][Number(field.isParentOf)]['values'] = section[field.isParentOf].dependentValues[val];
                 }
            }
        })
    })
    this.setState({
        commonTemplateFields:commonTemplateValues
    },()=>{
    this.props.updateFinalPojo('orderInfo',commonTemplateValues);

    })
}

commonTemplateFields 的正确值不会进入 setstate 即值的数组,而是作为初始状态为空。

PS:它通过了所有if条件。

PPS:对象结构:

    {  

   "values":[  
      "Flight Avionics (FLX)",
      "In-Flight Entertainment (IFE)",
      "Training & Simulation (T&S)",
      "Microwave & Imaging (MIS)"
   ],
   "parent":1,
   "parentValue":[  
      "depend"
   ],
   "dependentValues":{  
      "AVS - Avionics":[  
         "Flight Avionics (FLX)",
         "In-Flight Entertainment (IFE)",
         "Training & Simulation (T&S)",
         "Microwave & Imaging (MIS)"
      ],
      "DMS - Defence Mission Systems":[  
         "Above Water Systems (AWS)",
         "Electronic Combat Systems (ECS)",
         "Intelligence Surveillance & Reconnaissance (ISR)",
         "Under Water Systems (UWS)"
      ],

   },
   "isParentOf":"3"
}

标签: javascriptreactjsobject

解决方案


我想出了解决方案并将其发布,因为它可能对其他人也有帮助。当我们对嵌套对象进行循环时,它看起来好像只在第一级进行比较。我正在改变的属性是在第 3 级。也许我错了,但这是我在第一级进行另一次更改时得出的结论,第三级的值更改也开始显示。


推荐阅读