Lodash remove items recursively

前端 未结 6 731
难免孤独
难免孤独 2020-12-03 22:35

Given this JSON object, how does lodash remove the reach value from the objects?

{ 
    total: 350,
    SN1: { 
        reach: 200,
        enga         


        
6条回答
  •  北海茫月
    2020-12-03 23:12

    use _.mixin to extend a omitDeep method:

    _.mixin({
        'omitDeep': function(obj, predicate) {
            return _.transform(obj, function(result, value, key) {
                if (_.isObject(value)) {
                    value = _.omitDeep(value, predicate);
                }
                var doOmit = predicate(value, key);
                if (!doOmit) {
                    _.isArray(obj) ? result.push(value) : result[key] = value;
                }
            });
        }
    });
    
    var my = {
        "key1": {
            "key2": {
                "key3": [null, {
                    "key4": "string",
                    "key5": true,
                    "key6": null,
                    "key7": 8,
                    "key7": undefined
                }, null]
            }
        }
    };
    
    console.log(my);
    console.log("omit null:", _.omitDeep(my, _.isNull));
    console.log("omit undefined:", _.omitDeep(my, _.isUndefined));

提交回复
热议问题