【发布时间】:2021-09-18 21:16:23
【问题描述】:
我有 2 个不可变对象。结构如下图
const state = fromJS({
"a":"value1",
"b":{
"c":"value2"
},
"d":[ ],
"e":{
"f":"value3",
"g":{
"h":true
}
}
})
and
const updateVal = fromJS({
"b":{"c": "newValue"},
"e": {"g":{"h": false"}}
})
我想要的结果是
state = fromJS({
"a":"value1",
"b":{
"c":"newValue"
},
"d":[ ],
"e":{
"f":"value3",
"g":{
"h":false
}
}
})
我尝试了 mergeWith、mergeDeep 但总是得到结果
var state = fromJS({
"a":"value1",
"b":{
"c":"newValue" // this is updating as I have same new object structure
},
"d":[ ],
"e":{ // here I am loosing other values
"g":{
"h":false
}
}
})
所以“e”中的所有内容都将替换为“updateVal”
我尝试过的
state.mergeWith((prev, next) => {
if(!prev) return next;
return next;
}, updateVal)
这只是一个结构,“updateVal”是动态的,我不知道 updateVal 会出现什么。所以如果某些结构匹配然后替换那些特定的值
【问题讨论】:
-
预期的输出有格式错误,我不确定在哪里。你能不能修一下。另外,请将确切的版本命名为 uf immutable.js。一些合并函数的行为从 3.8.x 到 4.0.x 略有变化
标签: javascript merge immutable.js