使用 Ramda 将键的值提升到顶层,同时保留对象的其余部分
Lift a key’s values to top level whilst preserving rest of object using Ramda
我想将它构建到我的 compose
函数中,以便 record
的值到达对象的顶层,而其余键保持完整:
{
record: {
seasons: [
1
],
colors: [
2
]
},
tag_ids: [
2091
]
}
我要的结果:
{
seasons: [
1
],
colors: [
2
],
tag_ids: [
2091
]
}
任何键可能或可能不存在。
我一直对在 compose
函数中使用 ramda 方法感到困惑。目前,我正在查看 toPairs
并进行了一些相当冗长的转换,但没有成功。
您可以使用展开运算符。
const startingWithAllProps = {
record: {
seasons: [
1
],
colors: [
2
]
},
tag_ids: [
2091
]
}
const startingWithoutRecord = {
tag_ids: [
2091
]
}
const startingWithoutTagIds = {
record: {
seasons: [
1
],
colors: [
2
]
}
}
const moveRecordUpOneLevel = (startingObject) => {
const temp = {
...startingObject.record,
tag_ids: startingObject.tag_ids
}
return JSON.parse(JSON.stringify(temp)) // To remove any undefined props
}
const afterTransformWithAllProps = moveRecordUpOneLevel(startingWithAllProps)
const afterTransformWithoutRecord = moveRecordUpOneLevel(startingWithoutRecord)
const afterTransformWithoutTagIds = moveRecordUpOneLevel(startingWithoutTagIds)
console.log('afterTransformWithAllProps', afterTransformWithAllProps)
console.log('afterTransformWithoutRecord', afterTransformWithoutRecord)
console.log('afterTransformWithoutTagIds', afterTransformWithoutTagIds)
这在纯 JS 中可能比 Ramda 更简单:
const data = { record: { seasons: [1], colors: [2] }, tag_ids: [2091] }
const flattenRecord = ({record = {}, ...rest}) => ({...record, ...rest})
flattenRecord(data) //=> {"colors": [2], "seasons": [1], "tag_ids": [2091]}
如果您仍想使用 Ramda 作为解决方案,请考虑查看 R.mergeLeft
(or R.mergeRight
) and R.omit
。
您可以将 R.chain 与 R.merge 和 R.prop 结合使用,通过将键的内容与原始对象合并来展平键的内容,然后您可以省略原始键。
const { pipe, chain, merge, prop, omit } = R
const fn = key => pipe(
chain(merge, prop(key)), // flatten the key's content
omit([key]) // remove the key
)
const data = { record: { seasons: [1], colors: [2] }, tag_ids: [2091] }
const result = fn('record')(data)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>
这可能会有所帮助!
const lift = key => R.converge(R.mergeRight, [
R.dissoc(key),
R.prop(key),
]);
const liftRecord = lift('record');
// ====
const data = {
record: {
seasons: [
1
],
colors: [
2
]
},
tag_ids: [
2091
]
};
console.log(
liftRecord(data),
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js" integrity="sha256-buL0byPvI/XRDFscnSc/e0q+sLA65O9y+rbF+0O/4FE=" crossorigin="anonymous"></script>
我想将它构建到我的 compose
函数中,以便 record
的值到达对象的顶层,而其余键保持完整:
{
record: {
seasons: [
1
],
colors: [
2
]
},
tag_ids: [
2091
]
}
我要的结果:
{
seasons: [
1
],
colors: [
2
],
tag_ids: [
2091
]
}
任何键可能或可能不存在。
我一直对在 compose
函数中使用 ramda 方法感到困惑。目前,我正在查看 toPairs
并进行了一些相当冗长的转换,但没有成功。
您可以使用展开运算符。
const startingWithAllProps = {
record: {
seasons: [
1
],
colors: [
2
]
},
tag_ids: [
2091
]
}
const startingWithoutRecord = {
tag_ids: [
2091
]
}
const startingWithoutTagIds = {
record: {
seasons: [
1
],
colors: [
2
]
}
}
const moveRecordUpOneLevel = (startingObject) => {
const temp = {
...startingObject.record,
tag_ids: startingObject.tag_ids
}
return JSON.parse(JSON.stringify(temp)) // To remove any undefined props
}
const afterTransformWithAllProps = moveRecordUpOneLevel(startingWithAllProps)
const afterTransformWithoutRecord = moveRecordUpOneLevel(startingWithoutRecord)
const afterTransformWithoutTagIds = moveRecordUpOneLevel(startingWithoutTagIds)
console.log('afterTransformWithAllProps', afterTransformWithAllProps)
console.log('afterTransformWithoutRecord', afterTransformWithoutRecord)
console.log('afterTransformWithoutTagIds', afterTransformWithoutTagIds)
这在纯 JS 中可能比 Ramda 更简单:
const data = { record: { seasons: [1], colors: [2] }, tag_ids: [2091] }
const flattenRecord = ({record = {}, ...rest}) => ({...record, ...rest})
flattenRecord(data) //=> {"colors": [2], "seasons": [1], "tag_ids": [2091]}
如果您仍想使用 Ramda 作为解决方案,请考虑查看 R.mergeLeft
(or R.mergeRight
) and R.omit
。
您可以将 R.chain 与 R.merge 和 R.prop 结合使用,通过将键的内容与原始对象合并来展平键的内容,然后您可以省略原始键。
const { pipe, chain, merge, prop, omit } = R
const fn = key => pipe(
chain(merge, prop(key)), // flatten the key's content
omit([key]) // remove the key
)
const data = { record: { seasons: [1], colors: [2] }, tag_ids: [2091] }
const result = fn('record')(data)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>
这可能会有所帮助!
const lift = key => R.converge(R.mergeRight, [
R.dissoc(key),
R.prop(key),
]);
const liftRecord = lift('record');
// ====
const data = {
record: {
seasons: [
1
],
colors: [
2
]
},
tag_ids: [
2091
]
};
console.log(
liftRecord(data),
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js" integrity="sha256-buL0byPvI/XRDFscnSc/e0q+sLA65O9y+rbF+0O/4FE=" crossorigin="anonymous"></script>