[英]Immutable, update inside map not returning correct object
我正在尝试编写一个函数来更新我拥有的不可变对象。 我正在使用return myitem.updateIn
因此我可以在已经运行的这个更新的基础上链接另一个更新。 到目前为止,我有这个:
memo.updateIn(['Topics', 'filters'], function(topic) {
topic.map(function(singleTopic) {
singleTopic.update('filters', function(subTopicFilters) {
subTopicFilters.map(function(singleSubTopic) {
if(singleSubTopic.get('checked')){
console.log("check", singleTopic.toJS());
return singleTopic.set('checked', true);
}
})
})
})
});
控制台中的日志已经正确了,但是这似乎并没有像我想象的那样更新不可变映射。 psycological disorders
的checked
值应设置为true。 请参阅此处的小提琴,例如https://jsfiddle.net/alexjm/c5edxaue/27/ 。
在某些情况下,这是在返回中使用的,其中将在备忘录上依次运行几个单独的.update
,如下所示
returnModifiedData(memo) {
return memo.update (....
).update( .....
.update();
此功能是此过程的第一步,其他两个已经在工作。 我不确定自己做错了什么,无法正确进行更新,可能是我试图在.set
singletopic内部? 基本逻辑是检查主题内部是否有和子主题都已选中,如果是,则选中该主题。 任何帮助将不胜感激。 谢谢!
编辑 :忘记添加备忘录本身看起来像:
const memo = {
"Topics": {
"filters": {
"Psychological disorders": {
"checked": false,
"filters": {
"Anxiety disorders": {
"filters": {},
"checked": true
}
}
},
"test": {
"checked": false,
"filters": {
"test": {
"filters": {},
"checked": false
}
}
}
},
"isOpen": false
}
};
最好能解释一下要实现的逻辑。
我猜在这里:
遍历并更新Topics->filters
项目。
对于每个singleTopic
迭代的singleTopic
,进一步对其filters
进行迭代。
如果其singleSubTopic
任何一个均已checked
为true
,则将singleTopic
的checked
更新为true
。
以下是您可能期望的结果:
const map = { "Topics": { "filters": { "Psychological disorders": { "checked": false, "filters": { "Anxiety disorders": { "filters": {}, "checked": true } } }, "test": { "checked": false, "filters": { "test": { "filters": {}, "checked": false } } } }, "isOpen": false } }; let memo = Immutable.fromJS(map); memo = memo.updateIn(['Topics', 'filters'], function(topics) { // Remember to return the updated topics. return topics.map(function(singleTopic) { // If singleTopic has any of its singleSubTopic under filters have value checked=== true // update the singleTopic's checked, otherwise return unaltered. if (singleTopic.get('filters').some(function(singleSubTopic) { return singleSubTopic.get('checked'); })) { return singleTopic.set('checked', true); } return singleTopic; }); }); console.log(memo.toJS());
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.1/immutable.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.