Aayushi
Aayushi

Reputation: 1816

Accessing nested properties with dynamic property name

I have an object that is produced from this 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? I want to run a for loop through the keys present in the "Monthly time series" key. How can I do that?

Upvotes: 4

Views: 3497

Answers (2)

eithed
eithed

Reputation: 4320

You can access the keys the very same way as you're accessing them now through Object.keys function.

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:

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

i will contain your key


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)

Upvotes: 1

Yosvel Quintero
Yosvel Quintero

Reputation: 19070

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().

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

Upvotes: 2

Related Questions