javascript - 从嵌套对象中删除空值和 null 值 (ES6) - 清理嵌套对象

标签 javascript ecmascript-6

我得到了一个看起来像这样的对象:

{
    "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 值。

到目前为止,我使用此函数来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;
};

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

最终结果应为不含 kl & mqrl:

{
    "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 的所有值。

我想使用 ES6 方法增强这个 removeNull 的方法,但到目前为止我还没有做到。

我还尝试了用于此目的的老式方法 How to deeply remove null values, empty objects and empty array from an object

itemToBool = item => {
  if (typeof item !== 'object' || item === null) return item;
  const cleanedItem = cleanObject(item);
  return Object.keys(cleanedItem).length !== 0 && cleanedItem;
};

cleanObject = obj => {
  if (Array.isArray(obj)) {
    const newArr = obj.map(itemToBool).filter(Boolean);
    return newArr.length && newArr;
  }
  const newObj = Object.entries(obj).reduce((a, [key, val]) => {
    const newVal = itemToBool(val);
    if (newVal !== null || newVal === false) a[key] = newVal;
    return a;
  }, {});
  return Object.keys(newObj).length > 0 && newObj;
};

但它也失败了。

最佳答案

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

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

关于javascript - 从嵌套对象中删除空值和 null 值 (ES6) - 清理嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52367849/

相关文章:

javascript - 在 Map.prototype.forEach() 中获取索引

javascript - 如何在使用 mousemove 函数时在 div 中定义特定边界

javascript - 如何为leaflet.js设置特定比例

javascript - 需要一些帮助来理解 JS 模块模式的基础知识

javascript - 创建搜索栏以将数组过滤到表中

javascript - PhpStorm 中的传播运算符

javascript - 从现有数组中简化和排序新数组的创建

javascript - 如何检测浏览器是否兼容ES2015

javascript - 如何增加chrome ajax调用中的调用堆栈?

javascript - 检查图像是否为有效的 PNG 文件