代码之家  ›  专栏  ›  技术社区  ›  Pawan

如何删除嵌套数组中存在的名称属性未定义的JSON对象

  •  0
  • Pawan  · 技术社区  · 6 年前

    [
      {
        "forums": "",
        "resource": {
          "dhjName": "myvhp",
          "dhj": {
            "areaProgramValue": "123",
            "areaQuotas": [
              {
                "areaQuotaValue": "1234",
                "name": "acc"
              },
              {
                "areaQuotaValue": "12345",
                "name": "pro"
              }
            ],
            "methodType": "DGH",
    
          }
        },
        "task": "create"
      },
      {
        "forums": "",
        "resource": {
          "dhjName": "myvhp",
          "dhj": {
            "areaProgramValue": "123",
            "areaQuotas": [
              {
                "areaQuotaValue": "1234",
                "name": "acc"
              },
              {
                "areaQuotaValue": "12345",
                "name": "pro"
              },
              {
                "areaQuotaValue": "5666"
              },
              {
                "areaQuotaValue": "7666"
              }
            ],
            "methodType": "DGH",
    
          }
        },
        "task": "create"
      },
    
    ]
    

    面积配额 对他们中的一些人来说 名称 属性不存在。

    我已经试过了,如下所示

    test = test.filter((obj) =>  typeof obj.resource.dhj.areaQuotas.name === 'undefined');
    

    这是我的小提琴

    https://jsfiddle.net/o2gxgz9r/65225/

    1 回复  |  直到 6 年前
        1
  •  3
  •   gagan    6 年前

    你可以用 forEach 随着 filter

    test.forEach(item => {
      item.resource.dhj.areaQuotas = item.resource.dhj.areaQuotas.filter(
        areaQuota => {
          return areaQuota.hasOwnProperty('name');
        }
      );
    });
    

    这是最新的小提琴: https://jsfiddle.net/o2gxgz9r/65246/