不可变,在地图内部更新不会返回正确的对象 [英] Immutable, update inside map not returning correct object

查看:46
本文介绍了不可变,在地图内部更新不会返回正确的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个函数来更新我拥有的不可变对象.我正在使用return myitem.updateIn,因此我可以在已经运行的此更新的基础上链接另一个更新.到目前为止,我有这个:

I am trying to write a function to update an immutable object I have. I am using return myitem.updateIn so i can chain another update ontop of this one that is already working. So far, I have this :

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/.

The console log inside is hitting the correct part, however this is does not seem to be updating the immutable map as I assumed it would have. The checked value in psycological disorders should be set to true. See fiddle here for example https://jsfiddle.net/alexjm/c5edxaue/27/ .

在某些情况下,这是在返回中使用的,在该返回中,将在备忘录上依次运行几个单独的.update

For some context, this is being used in a return where a couple of separate .update will be run on the memo in order like this

returnModifiedData(memo) {
   return memo.update (....

   ).update( .....

   .update();

此功能是此过程的第一步,其他两个已经在工作.我不确定自己做错了什么,无法正确更新它,可能是我尝试在.set内部的singletopic?基本逻辑是检查主题内部是否有和子主题都已选中,如果是,则选中该主题.任何帮助将不胜感激.谢谢!

This function is the first step in this process, the other 2 are already working. I am not sure what I am doing wrong to not get this to update correctly, possibly how I am trying to .set the singletopic inside? The basic logic is check if the topic has and sub topics with checked inside, and if so, check off the topic. Any help would be greatly appreciated. Thanks!

编辑:忘记添加备忘录本身的样子:

EDIT : forgot to add what the memo itself looks like :

const memo = {
  "Topics": {
    "filters": {
      "Psychological disorders": {
        "checked": false,
        "filters": {
          "Anxiety disorders": {
            "filters": {},
            "checked": true
          }
        }
      },
      "test": {
        "checked": false,
        "filters": {
          "test": {
            "filters": {},
            "checked": false
          }
        }
      }
    },
    "isOpen": false
  }
};

推荐答案

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

It'd be better if you can explain what's the logic you want to achieve.

我会在这里猜到

  1. 迭代并更新Topics->filters中的项目.

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

For each singleTopic iterated, further iterate through it's filters.

如果其任何singleSubTopiccheckedtrue,请将singleTopicchecked更新为true.

If any of its singleSubTopic have checked to be true, update the singleTopic's checked to be true.

以下是您可能期望的情况:

And below is what you may expect:

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>

这篇关于不可变,在地图内部更新不会返回正确的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆