与原始文件进行比较时,如何使用ramda从对象中提取已更改的属性

时间:2017-10-04 12:46:24

标签: javascript ramda.js

希望使用ramda,我想对配置进行更改,并使用它们更新用户配置,其中属性/ ies更改具有相同的旧值。

经过讨论。变化。我将保留一个更改对象,只有R.merge(原始,用户)。但是,我需要能够提取已更改的属性及其值,以便在用户配置时我可以将该对象合并到用户中。

示例:



var original = { name:'test', setting1:1, setting2:2 };
var toChange = R.clone(original);
var userConfig {};
/// edit toChange
toChange.setting1 = 2;
/// extract setting1 and value from toChange

R.difference(original,toChange) => should be { setting1:2 };
/// but this does not work,  no matter what way round i put original 
/// and toChange, need to extract that change / diff

/// so i can then
userConfig = R.merge(userConfig, R.difference(original,toChange)); // for example




1 个答案:

答案 0 :(得分:0)

找到一种方法来解决某人想要做同样的事情



var orig = {a: 1, b: 2, c: 3};
var edited = R.merge(R.clone(orig),{b:7});
var user = { d: 4 };

//check if left value == right value
//if not, return null
//on merge with

//wrap around reject if null

//wrap around a merge into user

user = R.merge(user, R.reject(R.isNil, 
R.mergeWith(function(l,r){
  return (R.equals(l,r)) ? null : l;
},edited,orig)));

console.log(user);

<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.24.1/ramda.min.js"></script>
&#13;
&#13;
&#13;