Javascript 过滤嵌套的JSON数据

Javascript 过滤嵌套的JSON数据,javascript,json,javascript-objects,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:

首先,让我解释一下我想要实现的目标

我希望获取从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",
      }
    }
我如何遍历这些数据,展平嵌套对象,并仅返回“type”的key/val

例如。。。我想退回这样的东西

    {
      "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",
      }
    }
任何帮助都将不胜感激


谢谢

您必须使用递归函数,因为您不知道对象的嵌套方式。查看此函数

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调用此函数,它会起作用。

发布的问题似乎根本不包含解决问题的方法。StackOverflow希望您能这样做,因为您的尝试有助于我们更好地了解您的需求。请编辑问题以显示您已尝试过的内容,以便说明您在某个问题中遇到的具体问题。有关更多信息,请参阅并使用。