遍历JSON对象以映射列名称和值

时间:2020-05-04 06:41:16

标签: javascript json

下面是我的JSON

{
"portfolioID": 3024,
"gridType": "OWNER",
"gridLayoutId": 4,
"totalRecordCount": 111,
"attributeMetaData": [
    {
        "attributeId": 94,
        "attributeName": "OWNERTYPE",
        "objectType": "OWNER",
        "filterValue1": "",
        "filterValue2": "",
        "filterOperator1": "",
        "filterOperator2": "",
        "columnSortOrder": 0,
        "sortType": "",
        "attributeDisplayName": "TYPE",
        "columnOrder": 1,
        "isGeneralAttribute": "true",
        "isIndustryAttribute": "",
        "isCustomerAttribute": "",
        "dataSource": "Pega",
        "isHidden": "false",
        "isActionable": "",
        "actionType": "",
        "referenceAttribute": "",
        "attributeDataType": "STRING",
        "columnWidth": "",
        "isFreezable": ""
    },
    {
        "attributeId": 95,
        "attributeName": "OWNERSTATUS",
        "objectType": "OWNER",
        "filterValue1": "",
        "filterValue2": "",
        "filterOperator1": "",
        "filterOperator2": "",
        "columnSortOrder": 0,
        "sortType": "",
        "attributeDisplayName": "STATUS",
        "columnOrder": 2,
        "isGeneralAttribute": "true",
        "isIndustryAttribute": "",
        "isCustomerAttribute": "",
        "dataSource": "Pega",
        "isHidden": "false",
        "isActionable": "",
        "actionType": "",
        "referenceAttribute": "",
        "attributeDataType": "STRING",
        "columnWidth": "",
        "isFreezable": ""
    },
    {
        "attributeId": 93,
        "attributeName": "PREFERREDNAME",
        "objectType": "OWNER",
        "filterValue1": "",
        "filterValue2": "",
        "filterOperator1": "",
        "filterOperator2": "",
        "columnSortOrder": 1,
        "sortType": "ASC",
        "attributeDisplayName": "LICENSE OWNER NAME",
        "columnOrder": 3,
        "isGeneralAttribute": "true",
        "isIndustryAttribute": "",
        "isCustomerAttribute": "",
        "dataSource": "Pega",
        "isHidden": "false",
        "isActionable": "true",
        "actionType": "VIEWPAGE",
        "referenceAttribute": "LICENSEOWNERID",
        "attributeDataType": "STRING",
        "columnWidth": "",
        "isFreezable": ""
    },
    {
        "attributeId": 115,
        "attributeName": "LICENSEOWNERID",
        "objectType": "OWNER",
        "filterValue1": "",
        "filterValue2": "",
        "filterOperator1": "",
        "filterOperator2": "",
        "columnSortOrder": 0,
        "sortType": "",
        "attributeDisplayName": "LICENSEOWNERID",
        "columnOrder": 0,
        "isGeneralAttribute": "true",
        "isIndustryAttribute": "",
        "isCustomerAttribute": "",
        "dataSource": "Pega",
        "isHidden": "true",
        "isActionable": "",
        "actionType": "",
        "referenceAttribute": "",
        "attributeDataType": "INTEGER",
        "columnWidth": "",
        "isFreezable": ""
    }
],
"attributeValues": [
    {
        "objectId": 133218,
        "attributeList": [
            {
                "attributeId": 94,
                "attributeValue": "Entity",
                "referenceObjectId": null
            },
            {
                "attributeId": 95,
                "attributeValue": "Active",
                "referenceObjectId": null
            },
            {
                "attributeId": 93,
                "attributeValue": null,
                "referenceObjectId": "133218"
            },
            {
                "attributeId": 115,
                "attributeValue": "133218",
                "referenceObjectId": null
            }
        ]
    },
    {
        "objectId": 134179,
        "attributeList": [
            {
                "attributeId": 94,
                "attributeValue": "Individual",
                "referenceObjectId": null
            },
            {
                "attributeId": 95,
                "attributeValue": "Active",
                "referenceObjectId": null
            },
            {
                "attributeId": 93,
                "attributeValue": "Ra vi Teja",
                "referenceObjectId": "134179"
            },
            {
                "attributeId": 115,
                "attributeValue": "134179",
                "referenceObjectId": null
            }
        ]
    }
]
}

在JS以下尝试

 data.attributeValues.forEach((item, i) => {
const itemattrId = item.attributeList[i].attributeId;            
data.attributeMetaData.forEach((subItem, j) => {
  const subitemattrId = subItem.attributeId;
  if (itemattrId === subitemattrId) {
    console.log(subItem.attributeName + ' - ' + item.attributeList[i].attributeValue);
  }             
});
});

我想遍历此JSON以获取列名和列值的映射。在JSON中,映射是通过名为“ attributeId”的属性完成的。在“ attributeMetaData”中,“ attributeName”是列的名称,在“ attributeValues”中,存在多个“ attributeList”,并且每个属性都有“ attributeId”和“ attributeValue”属性。实际上,我正在尝试将对象构建为适当的名称值对,以将其绑定到网格。因此,基于“ attributeId”,我想映射每个名称值对。任何帮助将不胜感激。谢谢。

enter image description here

1 个答案:

答案 0 :(得分:0)

const idToNameMap = {};
obj.attributeMetaData.forEach(item => {
   idToNameMap[item.attributeId] = item;                               
})

const rows = obj.attributeValues.map(attributeObj => {
   const row = { objectId: attributeObj.objectId };
   attributeObj.attributeList.forEach(attr => { 
      if (idToNameMap[attr.attributeId].isHidden !== "true") {
         row[idToNameMap[attr.attributeId].attributeName] = attr.attributeValue;
      }
   });
   return row;
});

console.log(rows); //Prints:
//[
//  {
//    objectId: 133218,
//    OWNERTYPE: 'Entity',
//    OWNERSTATUS: 'Active',
//    PREFERREDNAME: null,
//  },
//  {
//    objectId: 134179,
//    OWNERTYPE: 'Individual',
//    OWNERSTATUS: 'Active',
//    PREFERREDNAME: 'Ra vi Teja',
//  }
//]