对象和对象数组

时间:2019-07-07 18:52:29

标签: javascript

"data": {
        "TripResults": {
          "Depth": [
            577,
            694,
            810
          ]          
        },
        "LastCalling": {
          "Last_Calling_Duration": 5699,
          "Last_Calling_Date": 20180619
        }
      }

这里是mData

      "mData": {
        "Depth": {
          "FieldName": "Depth"
        }
        "Last_Calling_Date": {
          "FieldName": "Last_Calling_Date"
        },
        "Last_Calling_Duration": {
          "FieldName": "Last_Calling_Duration"
        }
      }

以下代码处理得很好。

const entities = Object.entries(data).filter(([_, properties]) => properties !== null).map(([name, properties]) => ({
    name,
    view : viewObj.view,
    properties: Object.entries(properties)
        .filter(([propertyName]) => propertyName in mData)
        .map(([propertyName, value]) => ({
            propertyName,
            values: [].concat(value)
        }))
}));

但是,如果我得到以下格式为对象数组的数据输入,它将失败。我想知道如何处理这种情况。

  "data": {
        "TripResults" :[ 
              {
                "Depth": [
                        577,
                        694,
                        810
                    ]  
              }
         ]
     }

预期产量

{
  "entities": [
    {
      "name": "TripResults",
      "properties": [
        {
          "propertyName": "Depth",
          "values": [
            577,
            694,
            810
          ]
        }
      ]
    }
  ]
}

0 个答案:

没有答案