简体   繁体   English

如何使用 lodash 从 JSON 中递归删除空对象

[英]How to remove empty object from JSON recursively using lodash

var template = {
    personal: {},
    education: {},
    certificate: [{"test": "Test"}, {}, {}],
    experience: []
}

removeEmptyObj(template);

function removeEmptyObj(obj)
for (var key in obj) {
    console.log("Foor Loop" + key + " " + obj[key]);
    if (_.isObject(obj[key]) && !_.isEmpty(obj[key])) {
        console.log("Second Loop Object:::" + key + " " + obj[key]);
        removeEmptyObj(obj[key]);
    }
    if (_.isEmpty(obj[key])) {
        console.log("Delete Object:::" + key + " " + obj[key]);
        obj = _.omitBy(obj, _.isEmpty);
    }
}
console.log(obj);
return obj;
}

Current Output is : {certificate: [{"test": "Test"}, {}, {}]}当前输出是: {certificate: [{"test": "Test"}, {}, {}]}

Desired Output : {certificate: [{"test": "Test"}]}所需的输出: {certificate: [{"test": "Test"}]}

What's wrong here your help appreciate :)这里有什么问题,感谢您的帮助:)

You can _.transform() the object recursively to a new one, and clean empty objects and arrays on the way.您可以_.transform()递归地将对象转换为新对象,并在途中清理空对象和数组。

Note: I've added more elements to the structure for demonstration注意:为了演示,我在结构中添加了更多元素

 var template = { personal: {}, education: {}, certificate: [{}, {"test": "Test"}, {}, {}, [{}], [{}, 1, []]], experience: [[1, 2, [], { 3: [] }]] }; function clean(el) { function internalClean(el) { return _.transform(el, function(result, value, key) { var isCollection = _.isObject(value); var cleaned = isCollection ? internalClean(value) : value; if (isCollection && _.isEmpty(cleaned)) { return; } _.isArray(result) ? result.push(cleaned) : (result[key] = cleaned); }); } return _.isObject(el) ? internalClean(el) : el; } console.log(clean(template));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

You could use this function, making the distinction between plain objects and arrays:你可以使用这个函数,区分普通对象和数组:

 // Helper function var isEmptyObject = _.overEvery(_.isObject, _.isEmpty); // Actual function function removeEmptyObj(obj) { return _.isArray(obj) ? _.reject(_.map(obj, removeEmptyObj), isEmptyObject) : _.isObject(obj) ? _.omitBy(_.mapValues(obj, removeEmptyObj), isEmptyObject) : obj; } // Example var template = { personal: {}, education: {}, certificate: [{"test": "Test"}, {}, {}], experience: [] } var result = removeEmptyObj(template); console.log(result);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.4/lodash.min.js"></script>

No need for Lodash for that.不需要 Lodash。 A simple filter will do.一个简单的过滤器就可以了。

 var template = { personal: {}, education: {}, certificate: [{"test": "Test"}, {}, {}], experience: [] } template.certificate = template.certificate.filter(o => Object.keys(o).length) console.log(template.certificate)

Have you tried _.omit(certificate, [{}]).您是否尝试过 _.omit(certificate, [{}])。 I have never tried but let me know我从未尝试过,但请告诉我

You can try this method, it works for me with your example.你可以试试这个方法,用你的例子对我有用。 I'm pretty sure we can do in a better lodash way but at least you re unblock我很确定我们可以用更好的 lodash 方式做,但至少你可以解除封锁

deepOmit(obj) {
    function omitFromObject(obj) {
        return _.transform(obj, function(result, value, key) {
            if (_.isNull(value) || _.isUndefined(value) || _.isEmpty(value)) {
                return;
            }

            result[key] = _.isObject(value) ? omitFromObject(value) : value;
        });
    }

    return omitFromObject(obj);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM