删除空白嵌套对象(ES6)中的空值-清除嵌套对象 [英] Remove empty & null values from nested object (ES6) - Clean nested Objects

查看:981
本文介绍了删除空白嵌套对象(ES6)中的空值-清除嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的对象:

I got an object which looks like this :

{
    "a": "string not empty",
    "b": {
        "c": "string not empty",       
    },
    "d": {
        "e": false,
        "f": 0,
        "g": true,
        "h": 10
    },
    "i": {
        "j": 0,
        "k": null
    },
    "l": {
        "m": null
    },
    "n": {
        "o": 1,
        "p": "string (not empty)",
        "q": {}
    },
    "r": [],
    "l": "2000-01-01T01:01:00.000Z",
}

感谢此处提供的代码: https://stackoverflow.com/a/38364486/3912805 我可以现在删除嵌套对象的所有null值.

Thanks to the code provided by here : https://stackoverflow.com/a/38364486/3912805 I can now remove all null values of my nested object.

到目前为止,我已将此功能用于removeNull:

I used this function so far to removeNull :

removeNull = (obj) => {
  Object.keys(obj).forEach(key =>
    (obj[key] && typeof obj[key] === 'object') && removeNull(obj[key]) ||
    (obj[key] === undefined || obj[key] === null) && delete obj[key]
  );
  return obj;
};

但是我想增强此功能,以允许我删除嵌套对象中可能存在的所有空数组或任何空集合.

But I would like to enhance this function to allow me to remove all empty arrays or any empty collection which may exists in my nested object.

最终结果应为不包含 klmqrl:

Final results should be without k, l & m, q, r, l:

{
    "a": "string not empty",
    "b": {
        "c": "string not empty",       
    },
    "d": {
        "e": false,
        "f": 0,
        "g": true,
        "h": 10
    },
    "i": {
        "j": 0
    },
    "n": {
        "o": 1,
        "p": "string (not empty)"
    },
    "l": "2000-01-01T01:01:00.000Z",
}

我需要保留所有设置为0false的值.

I need to keep all values which were set to 0 or to false.

我想使用ES6方法来增强removeNull的方法,但是到目前为止我没有做到.

I would like to enhance this removeNull's method using ES6 method, but so far I failed to do it.

我还尝试了用于此但是它也失败了.

推荐答案

您可以通过直接迭代对象的键/值对并首先迭代嵌套的可迭代对象,然后删除不需要的键来采取直接方法.

You could take an straight forward approach by iterating the key/value pairs of the object and iterate nested iterable objects first and then delete the unwanted keys.

function clean(object) {
    Object
        .entries(object)
        .forEach(([k, v]) => {
            if (v && typeof v === 'object') {
                clean(v);
            }
            if (v && typeof v === 'object' && !Object.keys(v).length || v === null || v === undefined) {
                if (Array.isArray(object)) {
                    object.splice(k, 1);
                } else {
                    delete object[k];
                }
            }
        });
    return object;
}

var object = { a: "string not empty", b: { c: "string not empty" }, d: { e: false, f: 0, g: true, h: 10 }, i: { j: 0, k: null }, l: { m: null }, n: { o: 1, p: "string (not empty)", q: {} }, r: [{ foo: null }] };

console.log(clean(object));

.as-console-wrapper { max-height: 100% !important; top: 0; }

这篇关于删除空白嵌套对象(ES6)中的空值-清除嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆