在不使用镜头的情况下在 Ramda 中有条件地添加和重命名 属性
Add and rename property conditionally in Ramda without using a lense
我需要修复 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')
)
但在我看来,这会降低可读性。
我需要修复 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')
)
但在我看来,这会降低可读性。