Search code examples
javascriptevaljavascript-objectsramda.js

How can I use Ramda's omit() function on a deeply nested object member in javascript


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}}

Solution

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