Functional programming 是否有一种方法可以用Ramda覆盖对象中的prop name?
我有一个对象{thing1:{},thing2:{}}有没有一个方法可以只覆盖一个道具名称,比如{thing1:{},thing3not2:{}不确定是否有更快捷/更简单的方法,但是你可以结合assoc来添加新的键,结合dissoc来删除旧的键: const{curry,assoc,dissoc}=R; const renameProp=咖喱 旧名称、新名称、obj=> dissocoldName,assocnewName,obj[oldName],obj ; const myTransformation=renamePropthing2,thing3not2; 常量myResult=myTransformation{thing1:{},thing2:{}; console.logJSON.stringifymyResult,null,4;Functional programming 是否有一种方法可以用Ramda覆盖对象中的prop name?,functional-programming,ramda.js,Functional Programming,Ramda.js,我有一个对象{thing1:{},thing2:{}}有没有一个方法可以只覆盖一个道具名称,比如{thing1:{},thing3not2:{}不确定是否有更快捷/更简单的方法,但是你可以结合assoc来添加新的键,结合dissoc来删除旧的键: const{curry,assoc,dissoc}=R; const renameProp=咖喱 旧名称、新名称、obj=> dissocoldName,assocnewName,obj[oldName],obj ; const myTransfor
Ramda cookbook包含renameKeys函数 下面是从那里抄来的:
/**
* Creates a new object with the own properties of the provided object, but the
* keys renamed according to the keysMap object as `{oldKey: newKey}`.
* When some key is not found in the keysMap, then it's passed as-is.
*
* Keep in mind that in the case of keys conflict is behaviour undefined and
* the result may vary between various JS engines!
*
* @sig {a: b} -> {a: *} -> {b: *}
*/
const renameKeys = R.curry((keysMap, obj) =>
R.reduce((acc, key) => R.assoc(keysMap[key] || key, obj[key], acc), {}, R.keys(obj))
);
并称之为
renameKeys({thing2: 'thing3not2'}, {thing1: {}, thing2: {}})
=> {"thing1": {}, "thing3not2": {}}