首页 > 解决方案 > 通过 Ramda.js 复合 AND 条件拒绝数组项

问题描述

我想使用库来reject排列项目。Ramda.js

有必要删除具有两个等于 的道具的项目0without lambdas可以用普通Ramda.js函数做到这一点吗?

我希望收到带有A, B, C名称的项目(除了D、 whereaccbal都等于0),但我的示例返回A, C.

const arr = [
  {
    name: 'A',
    acc: 1,
    bal: 2,
  },
  {
    name: 'B',
    acc: 3,
    bal: 0,
  },
  {
    name: 'C',
    acc: 0,
    bal: 4,
  },
  {
    name: 'D',
    acc: 0,
    bal: 0,
  }
]

const filteredItems = R.reject(
  R.and(
    R.propEq('acc', 0),
    R.propEq('bal', 0)
  )
)(arr)

这是Ramda REPL 来测试我的示例

提前感谢您的帮助!

标签: javascriptramda.js

解决方案


使用R.allPass/ R.bothwith R.propEqis的替代方法R.where,它采用“规范对象”并在满足所有属性时返回 true。

R.reject(R.where({
  acc: R.equals(0),
  bal: R.equals(0)
}))

推荐阅读