从对象javascript es6获取价值

时间:2018-12-10 16:26:33

标签: javascript object

  let parameters = {
    "lifespanCount": 3,
    "parameters": {
      "fields": {
        "date.original": {
          "stringValue": "tomorrow",
          "kind": "stringValue"
        },
        "duration.original": {
          "stringValue": "3 days",
          "kind": "stringValue"
        },
        "date": {
          "structValue": {
            "fields": {
              "sys.date.recent.original": {
                "stringValue": "tomorrow",
                "kind": "stringValue"
              },
              "sys.date.original": {
                "stringValue": "tomorrow",
                "kind": "stringValue"
              },
              "sys.date": {
                "stringValue": "2018-12-11T12:00:00-05:00",
                "kind": "stringValue"
              },
              "sys.date.recent": {
                "stringValue": "2018-12-11T12:00:00-05:00",
                "kind": "stringValue"
              },
              "sys.date.partial": {
                "stringValue": "2018-12-11T12:00:00-05:00",
                "kind": "stringValue"
              },
              "sys.date.partial.original": {
                "stringValue": "tomorrow",
                "kind": "stringValue"
              }
            }
          },
          "kind": "structValue"
        },
        "yes.original": {
          "stringValue": "",
          "kind": "stringValue"
        },
        "duration": {
          "structValue": {
            "fields": {
              "sys.duration.object": {
                "structValue": {
                  "fields": {}
                },
                "kind": "structValue"
              },
              "sys.duration": {
                "structValue": {
                  "fields": {
                    "unit": {
                      "stringValue": "day",
                      "kind": "stringValue"
                    },
                    "amount": {
                      "numberValue": 3,
                      "kind": "numberValue"
                    }
                  }
                },
                "kind": "structValue"
              },
              "sys.duration.original": {
                "stringValue": "3 days",
                "kind": "stringValue"
              }
            }
          },
          "kind": "structValue"
        },
        "location.original": {
          "stringValue": "china",
          "kind": "stringValue"
        },
        "location": {
          "structValue": {
            "fields": {
              "sys.geo-country.original": {
                "stringValue": "china",
                "kind": "stringValue"
              },
              "sys.geo-country.object": {
                "structValue": {
                  "fields": {}
                },
                "kind": "structValue"
              },
              "sys.geo-country": {
                "stringValue": "China",
                "kind": "stringValue"
              }
            }
          },
          "kind": "structValue"
        }
      }
    }
  }

从对象上方获取值的最佳方法是什么,

let date = parameters.fields['date'].structValue.fields['sys.date'].stringValue;
let country = parameters.fields['location'].structValue.fields['sys.geo-country'].stringValue

什么是通用函数,所以只要传递对象和键,任何人都可以直接访问值 我尝试过object.values()object.keys()也已经阅读了MDN文档,但对此没有更多了解。我的目标是当我将任何键与对象一起传递时从对象获取值。

1 个答案:

答案 0 :(得分:0)

仅基于您的两个示例,最简单的方法似乎是:

and

根据结构其他变化的可能性,也许需要进行一些其他检查。

然后调用代码可以是:

function getString(paramField, structField) {
  if (parameters.fields[paramField] && parameters.fields[paramField].structValue.fields[structField]) {
  return parameters.fields[paramField].structValue.fields[structField].stringValue;
  } 
  return '';
}