简体   繁体   English

从对象javascript es6获取价值

[英]get value from object javascript es6

  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"
        }
      }
    }
  }

From above object what is the best way to get values like, 从上面的对象中获取值的最佳方法是什么,

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

what can be a common function so, anyone can access value directly if pass object and key i have tried object.values() , object.keys() have also read MDN docs but not getting much more regarding this. 什么可以是常见功能,所以任何人都可以直接访问值,如果我尝试传递object和key的object.values()object.keys()也阅读了MDN文档,但对此没有更多了解。 what my aim is to get the value from object when i pass any key with object. 我的目标是当我将任何键与对象一起传递时从对象获取值。

Based purely on your two examples, the easiest way seems to be: 纯粹基于您的两个示例,最简单的方法似乎是:

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

Maybe some additional checks are in order, depending on the likelihood of other variance to the structure. 也许要进行一些其他检查,具体取决于结构其他变化的可能性。

Then the calling code can just be: 然后,调用代码可以是:

let date = getString('date', 'sys.date');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM