Object: Deep omit

后端 未结 2 1797
北海茫月
北海茫月 2021-01-17 18:32

Is there a way to use _.omit on nested object properties?

I want this to happen:

schema = {
  firstName: {
    type: String
  },
  secre         


        
相关标签:
2条回答
  • 2021-01-17 18:43

    Detailed solution of this issue is posted in another thread. Please have a look at the below thread

    Link - Cleaning Unwanted Fields From GraphQL Responses

    0 讨论(0)
  • 2021-01-17 18:46

    You could create a nestedOmit mixin that would traverse the object to remove the unwanted key. Something like

    _.mixin({
        nestedOmit: function(obj, iteratee, context) {
            // basic _.omit on the current object
            var r = _.omit(obj, iteratee, context);
    
            //transform the children objects
            _.each(r, function(val, key) {
                if (typeof(val) === "object")
                    r[key] = _.nestedOmit(val, iteratee, context);
            });
    
            return r;
        }
    });
    

    and a demo http://jsfiddle.net/nikoshr/fez3eyw8/1/

    0 讨论(0)
提交回复
热议问题