我在递归函数中使用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.un来省略:
const { when, is, pipe, unless, omit, map } = R
const removeColors = obj => when(
is(Object),
pipe(
unless(is(Array), omit(['fill'])),
map(removeColors)
)
)(obj)
const result = removeColors({
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.without()
或R.without()
R.difference()
R.difference()