最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

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

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}}
Share Improve this question asked Sep 29, 2018 at 13:53 kennasoftkennasoft 1,5931 gold badge14 silver badges26 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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));

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

发布评论

评论列表(0)

  1. 暂无评论