javascript - 更改对象的更深层属性会显示在控制台中,但不会在我们打开它或稍后访问它时显示
问题描述
更改对象的更深层属性会显示在控制台中,但不会在我们稍后打开或访问它时显示。以下是我的对象:
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"
}
解决方案
我想出了解决方案并将其发布,因为它可能对其他人也有帮助。当我们对嵌套对象进行循环时,它看起来好像只在第一级进行比较。我正在改变的属性是在第 3 级。也许我错了,但这是我在第一级进行另一次更改时得出的结论,第三级的值更改也开始显示。
推荐阅读
- r - 根据 R / ggraph 中的 'y' 值垂直组织图节点
- python - 如何更改字符串中的特定字符?
- java - 无法在 child() 中为参数“pathString”传递 null 如何解决此错误
- php - 如何在 php + mysql 更新之前检查重复条目?
- r - 如何使用 R 的 summarytools 包中的 ctable 重命名行和列变量名称输出?
- c - 使用c中的指针解码传递给函数的2个元素数组
- python - 接受后存储好友请求(在数据库中)
- sql - SQL查询不选择两个表?
- r - 在R中使用条件语句取消过滤数据框
- javascript - 如何将编码为字节字符串的音频文件解码为 numpy 数组?