防止Ramda.omit将数组转换为对象

问题描述 投票:1回答:2

我在递归函数中使用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);
  }
javascript functional-programming ramda.js
2个回答
1
投票

除非对象不是数组,否则仅使用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>

1
投票

尝试使用R.without()R.without()

R.difference()
R.difference()
© www.soinside.com 2019 - 2024. All rights reserved.