[英]Using ramdajs for renaming properties of an object
我需要使用ramdajs重寫可能包含帶有連字符的單詞的對象的所有屬性,以camelCase的形式出現 。
例如,對於每個鍵,屬性名稱animation-timing-function
應該變為animationTimingFunction
等。
您能否提供一個例子:
這是我應該轉換的數據:
var data = { "bounce": [ { "animation-timing-function": "cubic-bezier(0.215, 0.610, 0.355, 1.000)", "transform": "translate3d(0,0,0)", "offset": 0 }, { "animation-timing-function": "cubic-bezier(0.215, 0.610, 0.355, 1.000)", "transform": "translate3d(0,0,0)", "offset": 0.2 }, { "animation-timing-function": "cubic-bezier(0.755, 0.050, 0.855, 0.060)", "transform": "translate3d(0, -30px, 0)", "offset": 0.4 }, { "animation-timing-function": "cubic-bezier(0.755, 0.050, 0.855, 0.060)", "transform": "translate3d(0, -30px, 0)", "offset": 0.43 }, { "animation-timing-function": "cubic-bezier(0.215, 0.610, 0.355, 1.000)", "transform": "translate3d(0,0,0)", "offset": 0.53 }, { "animation-timing-function": "cubic-bezier(0.755, 0.050, 0.855, 0.060)", "transform": "translate3d(0, -15px, 0)", "offset": 0.7 }, { "animation-timing-function": "cubic-bezier(0.215, 0.610, 0.355, 1.000)", "transform": "translate3d(0,0,0)", "offset": 0.8 }, { "transform": "translate3d(0,-4px,0)", "offset": 0.9 }, { "animation-timing-function": "cubic-bezier(0.215, 0.610, 0.355, 1.000)", "transform": "translate3d(0,0,0)", "offset": 1 } ] };
我嘗試修改此收據,但未成功:
const renameKeys = R.curry((keysMap, obj) => {
return R.reduce((acc, key) => {
acc[keysMap[key] || key] = obj[key];
return acc;
}, {}, R.keys(obj));
});
我不清楚您嘗試了什么。 有了該功能和少量基礎架構,即可將其應用於正確的對象,這似乎對我有用:
R.over(lensProp('bounce'), map(renameKeys({
'animation-timing-function': 'animationTimingFunction'
})), data)
//=> {bounce: [
// {
// animationTimingFunction: "cubic-bezier(0.215, 0.610, 0.355, 1.000)",
// offset: 0,
// transform: "translate3d(0,0,0)"
// },
// {
// animationTimingFunction: "cubic-bezier(0.215, 0.610, 0.355, 1.000)",
// offset: 0.2,
// transform: "translate3d(0,0,0)"
// },
// // ...
// ]}
但這似乎是名稱中唯一帶有連字符的鍵,所以也許我很困惑。 應該有其他人嗎?
您可以在Ramda REPL上看到這一點。
基於后續的評論,它看起來像在Ramda的菜譜進入前右您使用的是比較合適的一個:
const mapKeys = R.curry((fn, obj) =>
R.fromPairs(R.map(R.adjust(fn, 0), R.toPairs(obj)))
);
盡管Ramda不包含camelCase
函數,但是您可以在Web上找到它們,也可以很容易地創建自己的駱駝。 一個版本:
const camelCase = (str) => str.replace(/[-_]([a-z])/g,
(m) => m[1].toUpperCase()
)
有了這些,就更容易了:
R.over(lensProp('bounce'), map(mapKeys(camelCase)), data)
//=> {bounce: [
// {
// animationTimingFunction: "cubic-bezier(0.215, 0.610, 0.355, 1.000)",
// anotherHyphenatedEntry: "foo",
// offset: 0,
// transform: "translate3d(0,0,0)"
// },
// {
// animationTimingFunction: "cubic-bezier(0.215, 0.610, 0.355, 1.000)",
// offset: 0.2,
// transform: "translate3d(0,0,0)"
// },
// // ...
同樣,此版本在Ramda REPL中也可用。
此變體也重命名了所有嵌套鍵
const isObject = o => equals(type(o), 'Object')
const rKeys = fn => ifElse(isObject, renameKeys(fn), identity)
const mapper = fn => map(([key, value]) => [fn(key), rKeys(fn)(value)])
let renameKeys = curry((fn, obj) => compose(
fromPairs,
mapper(fn),
toPairs
)(obj))
從
{gender: {userinfo: 2, purchase: ['one', 'two'], key: {nestedKey: 1}}, anotherKey: {anotherAnotherKey: 3}}
至
{ANOTHERKEY: {ANOTHERANOTHERKEY: 3}, GENDER: {KEY: {NESTEDKEY: 1}, PURCHASE: ["one", "two"], USERINFO: 2}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.