简体   繁体   English

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

[英]Accessing nested properties with dynamic property name

I have an object that is produced from this 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"
            }
        }
    }
}

Using Object.keys() , I was able to get the key " Monthly time series ", but how can I access the keys within it? 使用Object.keys() ,我能够获得密钥“ 每月时间序列 ”,但我如何访问其中的密钥? I want to run a for loop through the keys present in the " Monthly time series " key. 我想通过“ 每月时间序列 ”键中的键运行for循环。 How can I do that? 我怎样才能做到这一点?

You can use Object.keys() to get the array of keys, but instead of for...loop I suggest to iterate over the array of keys with Array.prototype.forEach() . 您可以使用Object.keys()来获取键数组,但不是为了...循环,我建议使用Array.prototype.forEach()迭代键数组。

Code: 码:

 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']); }); 

You can access the keys the very same way as you're accessing them now through Object.keys function. 您可以通过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"]));

Output: 输出:

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

Ah, after your edit, if you want to loop through them, you don't need Object.keys function - you can simply iterate through the object like this: 啊,在编辑之后,如果你想循环它们,你不需要Object.keys函数 - 你可以简单地遍历对象,如下所示:

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

i will contain your key i会包含你的钥匙


Per comments: 每条评论:

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"]; });

To get one value from the array: 要从数组中获取一个值:

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

or: 要么:

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

to get all values (they won't be concated though) 获取所有值(虽然它们不会被合并)

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

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