使用 Ramda.js 按属性过滤对象

4

我刚开始使用 Ramda.js,想知道如何基于指定属性过滤对象。

查看R.filter,似乎只有_.filter传递对象的而不是属性。例如,REPL中给出的示例:

var isEven = (n, prop) => {
  console.log(typeof prop);

  // => 
  // undefined
  // undefined
  // undefined
  // undefined

  return n % 2 === 0;
}

R.filter(isEven, {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, d: 4}

如果我有以下对象:

如果我有以下对象:

const obj = {a: 1, b: 2, c: 3};

我的期望结果是:

const filterProp = (x) => /* some filter fn */;

filterProp('b')(obj);

// => {a: 1, c: 3};

我该如何使用Ramda过滤对象的属性?
2个回答

7

在查阅了Ramda文档后,我发现R.omit可以满足我的特定需求。

const obj = {a: 1, b: 2, c: 3};

R.omit(['b'], obj);

// => {a: 1, c: 3};

4
使用 pickBy 方法,可以根据键过滤集合。
const obj = {a: 1, b: 2, c: 3};
var predicate = (val, key) => key !== 'b';
R.pickBy(predicate, obj);
// => {a: 1, c: 3}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接