使用动态属性名访问嵌套属性

时间:2017-08-21 11:08:50

标签: javascript arrays json

我有一个由这个JSON生成的对象:

{
    "data":  {
        "Meta Data": {
            "1. Information": "Monthly Prices (open, high, low, close) and Volumes",
            "2. Symbol": "MSFT",
            "3. Last Refreshed": "2017-08-18",
            "4. Time Zone": "US/Eastern"
        },
        "Monthly Time Series": {
            "2017-08-18": {
                "1. open": "73.1000",
                "2. high": "74.1000",
                "3. low": "71.2800",
                "4. close": "72.4900",
                "5. volume": "285933387"
            },
            "2017-07-31": {
                "1. open": "69.3300",
                "2. high": "74.4200",
                "3. low": "68.0200",
                "4. close": "72.7000",
                "5. volume": "451248934"
            }
        }
    }
}

使用Object.keys(),我能够获得密钥" 每月时间序列"但是如何访问其中的密钥?我想通过" 每月时间序列"中存在的键运行for循环。键。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

您可以使用Object.keys()获取密钥数组,但不是为了...循环,我建议使用Array.prototype.forEach()迭代密钥数组。

代码:

const obj = {"data": {"Meta Data": {"1. Information": "Monthly Prices (open, high, low, close) and Volumes","2. Symbol": "MSFT","3. Last Refreshed": "2017-08-18","4. Time Zone": "US/Eastern"},"Monthly Time Series": {"2017-08-18": {"1. open": "73.1000","2. high": "74.1000","3. low": "71.2800","4. close": "72.4900","5. volume": "285933387"},"2017-07-31": {"1. open": "69.3300","2. high": "74.4200","3. low": "68.0200","4. close": "72.7000","5. volume": "451248934"}}}};

Object
  .keys(obj.data['Monthly Time Series'])
  .forEach(function (k) {
    console.log(obj.data['Monthly Time Series'][k]['1. open']);
  });

答案 1 :(得分:1)

您可以通过Object.keys功能以与访问密钥相同的方式访问密钥。

var data = JSON.parse('{"data": {\
    "Meta Data": {\
        "1. Information": "Monthly Prices (open, high, low, close) and Volumes",\
        "2. Symbol": "MSFT",\
        "3. Last Refreshed": "2017-08-18",\
        "4. Time Zone": "US/Eastern"\
    },\
    "Monthly Time Series": {\
        "2017-08-18": {\
            "1. open": "73.1000",\
            "2. high": "74.1000",\
            "3. low": "71.2800",\
            "4. close": "72.4900",\
            "5. volume": "285933387"\
        },\
        "2017-07-31": {\
            "1. open": "69.3300",\
            "2. high": "74.4200",\
            "3. low": "68.0200",\
            "4. close": "72.7000",\
            "5. volume": "451248934"\
        }\
}}}');

console.log(Object.keys(data['data']["Monthly Time Series"]));

输出:

(2) ["2017-08-18", "2017-07-31"]

啊,编辑之后,如果你想循环它们,你不需要Object.keys函数 - 你可以简单地遍历这个对象:

for (var i in data['data']['Monthly Time Series'])
    console.log(i, data['data']['Monthly Time Series'][i])

i将包含您的密钥

每条评论:

var obj = {"data": {
    "Meta Data": {
        "1. Information": "Monthly Prices (open, high, low, close) and Volumes",
        "2. Symbol": "MSFT",
        "3. Last Refreshed": "2017-08-18",
        "4. Time Zone": "US/Eastern"
    },
    "Monthly Time Series": {
        "2017-08-18": {
            "1. open": "73.1000",
            "2. high": "74.1000",
            "3. low": "71.2800",
            "4. close": "72.4900",
            "5. volume": "285933387"
        },
        "2017-07-31": {
            "1. open": "69.3300",
            "2. high": "74.4200",
            "3. low": "68.0200",
            "4. close": "72.7000",
            "5. volume": "451248934"
        }
}}}

Object.keys(obj.data["Monthly Time Series"]).map(function(key, index){ return obj.data["Monthly Time Series"][key]["1. open"]; });

从数组中获取一个值:

(2) ["73.1000", "69.3300"]

或:

Object.keys(obj.data["Monthly Time Series"]).map(function(key, index){ return Object.values(obj.data["Monthly Time Series"][key]); });

获取所有值(虽然它们不会合并)