6

I have a situation where I want to remove any part of an object tree before flattening and exporting to CSV. Ramda is my choice library for FP in JS, but I noticed the R.omit() function works at only one level deep of the target object. How can I make it such that I can do the following?

const R = require('ramda');

const obj = {
    id: 1,
    name: 'me',
    audience_sizes: {
        fb: 500,
        dfp: 2000,
        apn: 1800
    }
};

console.log(JSON.stringify(R.omit(['id', 'audience_sizes.fb'], obj)));

I would expect the following result:

{"name":"me","audience_sizes":{"dfp":2000, "apn": 1800}}
kennasoft
  • 1,595
  • 1
  • 14
  • 26

2 Answers2

9

I think Lenses is a more functional way of doing this.

R.over(R.lensProp('audience_sizes'), R.omit(['fb']), R.omit(['id'], obj));
Buggy
  • 3,539
  • 1
  • 21
  • 38
  • Thanks for this. I'm new to Ramda, and I was not aware of this function. Can solve a lot of problems for me! – kennasoft Sep 29 '18 at 19:14
7

DissocPath look like what you are looking for. Use it or lenses for more complex deep updates.

Ebuall
  • 1,306
  • 1
  • 12
  • 14