简体   繁体   English

在Map中设置ImmutableJS进行深度比较

[英]ImmutableJS set in Map with deep comparison

I have two deeply nested data objects, such that: 我有两个深度嵌套的数据对象,例如:

myMap.get("dataKey") === maybeNewData // false
Immutable.is(myMap.get("dataKey"), maybeNewData) //maybe true

I want to replace myMap.get("dataKey") with maybeNewData only if some of the nested values changed. 我想更换myMap.get(“dataKey”)与maybeNewData只有一些嵌套值的变化。 Is there a syntactically precise way to do this? 有没有一种语法上精确的方法来做到这一点? For example (method doesn't exist): 例如(该方法不存在):

const maybeNewMap = myMap.setIfDeepDifferent("dataKey", maybeNewData);

Or do I have to do it like this?: 还是我必须这样做?:

if (!Immutable.is(myMap.get("dataKey"), maybeNewData)) {
  maybeNewMap = myMap.set("dataKey", maybeNewData);
}

I found a solution which is slightly better: using update provides a place for the .is check: 我发现了一个更好的解决方案:使用update.is检查提供了一个位置:

const maybeNewMap = myMap.update("dataKey", (data) => {
  return !Immutable.is(data, maybeNewData) ? maybeNewData : data;
});

I'm still curious if there is a more succinct way to express this. 我仍然很好奇是否有一种更简洁的方式来表达这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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