我需要修复 ramda.js 中对象的属性。我不想使用镜头。
拥有我需要的以下数据:
如果 a
和 b
中的数组属性中的对象以及其他
没有属性“animationTimingFunction”,添加属性键“easing”,值为
ease
确实有属性“animationTimingFunction”,将此属性重命名为“easing”并保持值不变。
输入数据:
let data = {
"a": [{
"opacity": "1",
"offset": "0"
}, {
"opacity": "0",
"offset": "0.25",
"animationTimingFunction": "linear"
}, {
"opacity": "1",
"offset": "0.5"
},
"b": [{
"transform": "scale3d(1, 1, 1)",
"offset": "0"
}, {
"transform": "scale3d(1.05, 1.05, 1.05)",
"offset": "0.5"
}, {
"transform": "scale3d(1, 1, 1)",
"offset": "1"
}]
};
输出应该是:
let data = {
"a": [{
"opacity": "1",
"offset": "0",
"easing": "ease"
}, {
"opacity": "0",
"offset": "0.25",
"easing": "linear"
}, {
"opacity": "1",
"offset": "0.5",
"easing": "ease"
},
"b": [{
"transform": "scale3d(1, 1, 1)",
"offset": "0",
"easing": "ease"
}, {
"transform": "scale3d(1.05, 1.05, 1.05)",
"offset": "0.5",
"easing": "ease"
}, {
"transform": "scale3d(1, 1, 1)",
"offset": "1",
"easing": "ease"
}]
};
作为开始,我已经尝试过,但我错过了条件部分:
let convertEasing = (data) =>{
let convert = data => R.assoc('easing', 'linear');
let result = R.map(R.map(convert(data)), data)
return result;
};
最佳答案
就这样怎么样
const alter = obj => {
const ease = prop('animationTimingFunction', obj) || 'ease';
return dissoc('animationTimingFunction', assoc('easing', ease, obj));
}
map(map(alter))(data);
如果您愿意,您可以免积分:
const alter = pipe(
chain(
assoc('easing'),
pipe(prop('animationTimingFunction'), defaultTo('ease')),
),
dissoc('animationTimingFunction')
)
但在我看来,这会失去可读性。
关于javascript - 在 Ramda 中有条件地添加和重命名属性,无需使用镜头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43804965/