【问题标题】:Javascript or lodash nested JSON object transformationJavascript 或 lodash 嵌套 JSON 对象转换
【发布时间】:2021-07-15 04:04:47
【问题描述】:

如何使嵌套对象为空

如果对象属性具有空值。请检查下面的 JSON

我有如下 JSON 数据

[{
    "student": {
        "id": null,
        "name": null
    },
    "children": [{
        "student": null,
        "children": [{
                "student": {
                    "id": 1,
                    "name": "A"
                }
            },
            {
                "student": {
                    "id": null,
                    "name": null
                }
            }
        ]
    }]
}]


我想把它转换成下面的输出

Expected

[{
    "student": null,
    "children": [{
        "student": null,
        "children": [{
                "student": {
                    "id": 1,
                    "name": "A"
                }
            },
            {
                "student": null
            }
        ]
    }]
}]

【问题讨论】:

    标签: javascript json lodash


    【解决方案1】:

    如果它们的值全部为空,则可以使用以下条件“使值无效”。

    Object.values(obj).every(value => value == null)
    

    我在下面创建了一个基本的递归函数,它遍历对象并检查它是否需要使对象无效。这一切都是就地完成的,它会修改原始对象。

    const obj = [{
      "student": { "id": null, "name": null },
      "children": [{
        "student": null,
        "children": [
          { "student": { "id": 1, "name": "A" } },
          { "student": { "id": null, "name": null } }
        ]
      }]
    }];
    
    const nullify = (obj, key = null, parent = null) => {
      if (obj != null) {
        if (Array.isArray(obj)) {
          obj.forEach((item, index) => nullify(item, index, obj));
        } else if (typeof obj === 'object') {
          if (Object.values(obj).every(value => value == null)) {
            parent[key] = null;
          } else {
            Object.entries(obj).forEach(([key, value]) => nullify(value, key, obj));
          }
        }
      }
    };
    
    nullify(obj);
    
    console.log(obj);
    .as-console-wrapper { top: 0; max-height: 100% !important; }

    【讨论】:

      【解决方案2】:

      这是一个使用object-scan的迭代解决方案

      // const objectScan = require('object-scan');
      
      const data = [{ student: { id: null, name: null }, children: [{ student: null, children: [{ student: { id: 1, name: 'A' } }, { student: { id: null, name: null } }] }] }];
      
      const nullify = objectScan(['**(^children$).student'], {
        useArraySelector: false,
        rtn: 'count',
        filterFn: ({ parent, property, value }) => {
          if (
            value instanceof Object
            && !Array.isArray(value)
            && Object.values(value).every((e) => e === null)
          ) {
            parent[property] = null;
            return true;
          }
          return false;
        }
      });
      
      console.log(nullify(data));
      // => 2
      
      console.log(data);
      // => [ { student: null, children: [ { student: null, children: [ { student: { id: 1, name: 'A' } }, { student: null } ] } ] } ]
      .as-console-wrapper {max-height: 100% !important; top: 0}
      <script src="https://bundle.run/object-scan@14.3.1"></script>

      免责声明:我是object-scan的作者

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-15
        • 2021-12-06
        • 1970-01-01
        • 1970-01-01
        • 2021-10-08
        • 2019-07-04
        相关资源
        最近更新 更多