过滤嵌套的JSON数据

时间:2018-10-03 22:23:36

标签: javascript json javascript-objects

首先,让我解释一下我要完成的工作...

我想获取我从API接收的数据,并根据特定值对其进行过滤。我将在下面发布JSON ...

{ assetNo:
{ type: 'string',
 format: 'short-text',
 minLength: 0,
 maxLength: 100,
 description: 'Asset ID' },
 serialNo:
 { type: 'string',
 format: 'short-text',
 minLength: 0,
 maxLength: 100,
 description: 'Serial Number' },
 'item:model':
 { type: 'object',
 properties: { title: [Object], _id: [Object] },
 description: 'Model' },
 'location:location':
 { type: 'object',
 properties: { title: [Object], _id: [Object] },
 description: 'Location ID' },
 'item:status':
 { type: 'object',
 properties: { title: [Object], _id: [Object] },
 description: 'Status' },
 imei:
 { type: 'string',
 format: 'short-text',
 minLength: 0,
 maxLength: 100,
 description: 'IMEI' },
 hostName:
 { type: 'string',
 format: 'short-text',
 minLength: 0,
 maxLength: 100,
 description: 'Host Name' },
 staticIp:
 { type: 'string',
 format: 'short-text',
 minLength: 0,
 maxLength: 100,
 description: 'Static IP' },
 mac:
  { type: 'string',
 format: 'short-text',
 minLength: 0,
 maxLength: 100,
 description: 'MAC' },
 macFriendlyName:
 { type: 'string',
 format: 'short-text',
 minLength: 0,
 maxLength: 100,
 description: 'MAC Friendly Name' },
 manufacturerWarrantyEndDate:
 { type: 'string',
 format: 'date-time',
 description: 'Manufacturer Warranty End Date' },
 notes:
 { type: 'string',
 format: 'long-text',
 minLength: 0,
 maxLength: 100,
 description: 'Notes' },
 _id:
 { type: 'string',
 format: 'BSON ObjectID',
 example: '12344ac4ac34d504212c0db6' } }

如您所见,有一些嵌套的对象...被展平,数据看起来像这样...

    {
      "assetNo": {
        "type": "string",
        "format": "short-text",
        "minLength": 0,
        "maxLength": 100,
        "description": "Asset ID"
      },
      "serialNo": {
        "type": "string",
        "format": "short-text",
        "minLength": 0,
        "maxLength": 100,
        "description": "Serial Number"
      },
      "item:model": {
        "type": "object",
        "properties": {
          "title": {
            "type": "string",
            "format": "short-text",
            "description": "title"
          },
          "_id": {
            "type": "string",
            "format": "BSON ObjectID",
            "example": "12344ac4ac34d504212c0db6"
          }
        },
        "description": "Model"
      },
      "location:location": {
        "type": "object",
        "properties": {
          "title": {
            "type": "string",
            "format": "short-text",
            "description": "title"
          },
          "_id": {
            "type": "string",
            "format": "BSON ObjectID",
            "example": "12344ac4ac34d504212c0db6"
          }
        },
        "description": "Location ID"
      },
      "item:status": {
        "type": "object",
        "properties": {
          "title": {
            "type": "string",
            "format": "short-text",
            "description": "title"
          },
          "_id": {
            "type": "string",
            "format": "BSON ObjectID",
            "example": "12344ac4ac34d504212c0db6"
          }
        },
        "description": "Status"
      },
      "imei": {
        "type": "string",
        "format": "short-text",
        "minLength": 0,
        "maxLength": 100,
        "description": "IMEI"
      },
      "hostName": {
        "type": "string",
        "format": "short-text",
        "minLength": 0,
        "maxLength": 100,
        "description": "Host Name"
      },
      "staticIp": {
        "type": "string",
        "format": "short-text",
        "minLength": 0,
        "maxLength": 100,
        "description": "Static IP"
      },
      "mac": {
        "type": "string",
        "format": "short-text",
        "minLength": 0,
        "maxLength": 100,
        "description": "MAC"
      },
      "macFriendlyName": {
        "type": "string",
        "format": "short-text",
        "minLength": 0,
        "maxLength": 100,
        "description": "MAC Friendly Name"
      },
      "manufacturerWarrantyEndDate": {
        "type": "string",
        "format": "date-time",
        "description": "Manufacturer Warranty End Date"
      },
      "notes": {
        "type": "string",
        "format": "long-text",
        "minLength": 0,
        "maxLength": 100,
        "description": "Notes"
      },
      "_id": {
        "type": "string",
        "format": "BSON ObjectID",
        "example": "12344ac4ac34d504212c0db6"
      }
    }

我该如何遍历此数据,展平嵌套的对象并仅返回“类型”的键/值?

例如...我想返回看起来像这样的东西...

    {
      "assetNo": {
        "type": "string",
      },
      "serialNo": {
        "type": "string",
      },
      "location:location": {
        "type": "object",
            "type": "string",
          },
          "_id": {
            "type": "string",
      },
      "item:status": {
        "type": "object",
            "type": "string",
          },
          "_id": {
            "type": "string",
          }
      },
      "imei": {
        "type": "string",
      },
      "hostName": {
        "type": "string",
      },
      "staticIp": {
        "type": "string",
      },
      "mac": {
        "type": "string",
      },
      "macFriendlyName": {
        "type": "string",
      },
      "manufacturerWarrantyEndDate": {
        "type": "string",
      },
      "notes": {
        "type": "string",
      },
      "_id": {
        "type": "string",
      }
    }

任何帮助将不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:0)

您将必须使用递归函数,因为您不会嵌套该对象。看看这个功能。

const mapObj = (data) => {
return Object.keys(data).reduce((agg, ele) => {
    if (ele === 'type') {
        agg.type = data[ele];
    } else if (typeof data[ele] === 'object') {
        agg[ele] = mapObj(data[ele]);
    }
    return agg;
}, new Object());

};

使用您的json调用此函数,它将成功解决问题。