How to filter out specific keys from object using Ramda?

Leon Gaban picture Leon Gaban · Apr 10, 2017 · Viewed 12.4k times · Source

http://ramdajs.com/0.21.0/docs/#prop

Ramda Repl

var myObject = {a: 1, b: 2, c: 3, d: 4};

var newObject = R.filter(R.props('a'), myObject);
//var newObject = R.filter(R.equals(R.props('a')), myObject);

console.log('newObject', newObject);

Right now the code above is returning the entire object:

newObject {"a":1,"b":2,"c":3,"d":4}

What I would like to do is just return a new object with just the 'a' key. Or a new object with the a and b keys.

Answer

Jared Smith picture Jared Smith · Apr 10, 2017

Use pick:

let newObj = R.pick(['a'], oldObj);

If your filtering criteria is more complex than just existence, you can use pickBy to select via arbitrary predicate functions.