不可变的,在地图内部更新不返回正确的对象
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。例如,参见 fiddle 此处 https://jsfiddle.net/alexjm/c5edxaue/27/ 。
在某些情况下,这被用在 return 中,其中几个单独的 .update
将 运行 放在备忘录中,顺序如下
returnModifiedData(memo) {
return memo.update (....
).update( .....
.update();
这个功能是这个过程的第一步,其他两个已经在工作了。我不确定我做错了什么不能让它正确更新,可能我是如何尝试 .set
里面的单主题的?基本逻辑是检查topic里面是否有checked和子topic,如果有就把topic勾掉。任何帮助将不胜感激。谢谢!
编辑:忘记添加备忘录本身的样子:
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
迭代,进一步迭代它是 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>
我正在尝试编写一个函数来更新我拥有的不可变对象。我正在使用 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。例如,参见 fiddle 此处 https://jsfiddle.net/alexjm/c5edxaue/27/ 。
在某些情况下,这被用在 return 中,其中几个单独的 .update
将 运行 放在备忘录中,顺序如下
returnModifiedData(memo) {
return memo.update (....
).update( .....
.update();
这个功能是这个过程的第一步,其他两个已经在工作了。我不确定我做错了什么不能让它正确更新,可能我是如何尝试 .set
里面的单主题的?基本逻辑是检查topic里面是否有checked和子topic,如果有就把topic勾掉。任何帮助将不胜感激。谢谢!
编辑:忘记添加备忘录本身的样子:
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
迭代,进一步迭代它是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>