繁体   English   中英

不可变,在地图内部更新不会返回正确的对象

[英]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 disorderschecked值应设置为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
  }
};

最好能解释一下要实现的逻辑。

我猜在这里:

  1. 遍历并更新Topics->filters项目。

  2. 对于每个singleTopic迭代的singleTopic ,进一步对其filters进行迭代。

  3. 如果其singleSubTopic任何一个均已checkedtrue ,则将singleTopicchecked更新为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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM