[英]Prevent Ramda.omit from converting arrays into objects
我在递归 function 中使用R.omit
传递它两个对象和 arrays。 将数组传递给它时会出现问题,因为它被转换为 object:
const shouldBeArray = R.omit(['fill'])(['bla']);
这里shouldBeArray
变成{ '0': 'bla' }
如何将 arrays 保留为 arrays? 这在 javascript 的上下文中没有太大区别,但是当运行 object 到JSON.stringify
时,结构变得明显不同。
这是整个 function:
function removeColors(svgObj) {
return R.when(
R.is(Object),
R.pipe(
R.omit(['fill']),
R.map(removeColors)
)
)(svgObj);
}
使用 R.unless 仅当 object 不是数组时才省略:
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.without()
或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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.