阻止 Ramda.omit 将数组转换为对象
Prevent Ramda.omit from converting arrays into objects
我在向它传递对象和数组的递归函数中使用 R.omit
。将数组传递给它时会出现问题,因为它会转换为对象:
const shouldBeArray = R.omit(['fill'])(['bla']);
这里shouldBeArray
变成了{ '0': 'bla' }
如何将数组保持为数组?这在 javascript 的上下文中没有太大区别,但是当 运行 对象通过 JSON.stringify
时,结构变得明显不同。
这是整个函数:
function removeColors(svgObj) {
return R.when(
R.is(Object),
R.pipe(
R.omit(['fill']),
R.map(removeColors)
)
)(svgObj);
}
尝试使用 R.without()
or R.difference()
console.log(R.without(['kromid'])(['bla']))
console.log(R.difference(['bla'])(['kromid']))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
使用R.unless仅当对象不是数组时才省略:
const { curry, when, is, pipe, unless, omit, map } = R
const omitDeep = curry((keys, obj) => when(
is(Object),
pipe(
unless(is(Array), omit(keys)),
map(omitDeep(keys))
)
)(obj))
const result = omitDeep(['fill'], {
arr: ['bla'],
p1: {
arr: [{ fill: 'blue', background: 'red' }],
fill: 'green'
},
fill: 'yellow'
})
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>
我在向它传递对象和数组的递归函数中使用 R.omit
。将数组传递给它时会出现问题,因为它会转换为对象:
const shouldBeArray = R.omit(['fill'])(['bla']);
这里shouldBeArray
变成了{ '0': 'bla' }
如何将数组保持为数组?这在 javascript 的上下文中没有太大区别,但是当 运行 对象通过 JSON.stringify
时,结构变得明显不同。
这是整个函数:
function removeColors(svgObj) {
return R.when(
R.is(Object),
R.pipe(
R.omit(['fill']),
R.map(removeColors)
)
)(svgObj);
}
尝试使用 R.without()
or R.difference()
console.log(R.without(['kromid'])(['bla']))
console.log(R.difference(['bla'])(['kromid']))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
使用R.unless仅当对象不是数组时才省略:
const { curry, when, is, pipe, unless, omit, map } = R
const omitDeep = curry((keys, obj) => when(
is(Object),
pipe(
unless(is(Array), omit(keys)),
map(omitDeep(keys))
)
)(obj))
const result = omitDeep(['fill'], {
arr: ['bla'],
p1: {
arr: [{ fill: 'blue', background: 'red' }],
fill: 'green'
},
fill: 'yellow'
})
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>