检查键是否存在于另一个对象中

时间:2019-07-02 20:53:05

标签: javascript

有两个对象metadatadata

我编写了以下代码来查找metadata对象中是否存在data对象密钥。

我想知道是否存在以下方法的缩写。

这里是data

      "data": {
        "TripResults": {
          "Depth": [
            577,
            694,
            810
          ],
          "Speed": [
            150,
            150,
            150
          ],
          "Frequency": [
            3,
            3,
            3
          ]
        },
        // the following two lines are added
        "Trincot": true,
        "Belgium": 2019,  
        "SurveyResults": {
          "Depth": [
            577,
            694,
            810
          ],
          "Speed": [
            150,
            150,
            150
          ],
          "Frequency": [
            3,
            3,
            3
          ]
        },
        "LastCalling": {
          "Last_Calling_Duration": 5699,
          "Last_Calling_Date": 20180619
        }
      }

这里是metadata

      "metaData": {
        "Depth": {
          "FieldName": "Depth"
        },
        "Time": {
          "FieldName": "Time"
        },
        "Frequency": {
          "FieldName": "Frequency"
        },
        "Speed": {
          "FieldName": "Speed"
        },
        "Last_Calling_Date": {
          "FieldName": "Last_Calling_Date"
        },
        "Last_Calling_Duration": {
          "FieldName": "Last_Calling_Duration"
        }
      }

这是我幼稚的方法:

var unitjson = {}
var tempArray = []
for (var key in metadata) {
    properties = []
    eachObject = []
    for (var datakey in data) {
        if (data[datakey] != null && data[datakey].hasOwnProperty(key)) {
            console.log("key exists");
            properties.push({ "propertyName": key, "values": data[datakey][key] })
            eachObject.push({ "name" : datakey, "properties": properties })
        }
    }
   tempArray.push(JSON.parse(JSON.stringify(eachObject)))
   unitjson["entities"] = JSON.parse(JSON.stringify(tempArray))
}

预期输出类似于:

{
  "entities": [
    {
      "name": "TripResults",
      "properties": [
        {
          "propertyName": "Depth",
          "values": [
            577,
            694,
            810
          ]
        },
        {
          "propertyName": "Speed",
          "values": [
            150,
            150,
            150
          ]
        }
      ]
    },
    {
      "name": "SurveyResults",
      "properties": [
        {
          "propertyName": "Depth",
          "values": [
            577,
            694,
            810
          ]
        },
        {
          "propertyName": "Speed",
          "values": [
            150,
            150,
            150
          ]
        }
      ]
    },
    {
      "name": "LastCalling",
      "properties": [
        {
          "propertyName": "Last_Calling_Date",
          "values": [
            20180619
          ]
        },
        {
          "propertyName": "Last_Calling_Duration",
          "values": [
            5699
          ]
        }
      ]
    }
  ]
}

2 个答案:

答案 0 :(得分:3)

我将首先迭代data而不是metaData,因为后者实际上只是一个集合,...没有必要对其进行迭代。

然后检查data中每个嵌套对象的属性是否与元数据属性匹配,如果是,则为其创建一个条目。

我建议使用一种 functional 方法,该方法在很大程度上依赖于Object.entriesfiltermap

这是它的外观:

const data = {"TripResults": {"Depth": [577,694,810],"Speed": [150,150,150],"Frequency": [3,3,3]},"SurveyResults": {"Depth": [577,694,810],"Speed": [150,150,150],"Frequency": [3,3,3]},"LastCalling": {"Last_Calling_Duration": 5699,"Last_Calling_Date": 20180619}}
const metaData = {"Depth": { "FieldName": "Depth" },"Time": { "FieldName": "Time" },"Frequency": {"FieldName": "Frequency" },"Speed": { "FieldName": "Speed" },"Last_Calling_Date": { "FieldName": "Last_Calling_Date" },"Last_Calling_Duration": { "FieldName": "Last_Calling_Duration" }};

const entities = Object.entries(data).map(([name, properties]) => ({ 
    name, 
    properties: Object.entries(properties)
                .filter(([propertyName]) => propertyName in metaData)
                .map(([propertyName, value]) => ({
                        propertyName,
                        values: [].concat(value)
                }))
}));

console.log(entities);

答案 1 :(得分:1)

这是另一种方法。也许不是您希望的简写。

const getEntities = (metadata, data) => Object.keys(data).map((dataKey) => {

    const dataEntity = data[dataKey];

    return {
        name: dataKey,
        properties: Object.keys(metadata).reduce((result, metadataKey) => {
            if (dataEntity[metadataKey] != null && dataEntity.hasOwnProperty(metadataKey)) {
                result.push({
                    propertyName: metadataKey,
                    values: dataEntity[metadataKey]
                })
            } // implied else... skip it cuss the prop doesn't match

            return result;
        }, [])
    };
});