Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Accessing the first element of each nested object

I'm trying to access the date of each of the nested objects so that it prints when call a function. The Object is as follows:

{

    "2015-12": {
        "enabled": 1261394905,
        "down": 0,
        "uptime": 100
    },
    "2016-01": {
        "enabled": 2678400000,
        "down": 294616,
        "uptime": 99.989
    },
    "2016-02": {
        "enabled": 2505600000,
        "down": 294572,
        "uptime": 99.988
    },
    "2016-03": {
        "enabled": 2678400000,
        "down": 545954,
        "uptime": 99.98
    },
    "2016-04": {
        "enabled": 2592000000,
        "down": 0,
        "uptime": 100
    },
    "2016-05": {
        "enabled": 2678400000,
        "down": 0,
        "uptime": 100
    },
    "2016-06": {
        "enabled": 2592000000,
        "down": 5358367,
        "uptime": 99.793
    },
    "2016-07": {
        "enabled": 1848549928,
        "down": 0,
        "uptime": 100
    },
    "total": {
        "enabled": 18834744833,
        "down": 6493509,
        "uptime": 99.966
    }
}

I am attempting to print the words / date "total", "2016-07", "2016-06" etc.

I can access all of the keys and values within each object but not the "title" of each of the nested Objects.

Here is an example of my code:

//OUTPUT THE UPTIME


function outputUptime(enabled, down, uptime) {
    "use strict";
    var response = xhr.responseText,
        key,
        uptimeArray = JSON.parse(response);


    document.getElementById("textDiv").innerHTML = "<div class ='textDiv'>" + "<button id='backtochecks' onclick='getallChecks()'>Back</button>" + "<h1>Uptime Monitor</h1>";

    for (key in uptimeArray) {
        // THIS IS WHERE I WOULD LIKE TO ACCESS THE TITLE STRING OF EACH OBJECT
        document.getElementById("textDiv").innerHTML +=  "<h3>" + uptimeArray.id + "</h3>" 

        + "<strong>The check was enabled for:</strong></br> " + uptimeArray[key].enabled + "</br>"
        + "<strong>DOWN:</strong> " + uptimeArray[key].down + "</br>"
        + "<strong>Uptime: </strong>" + uptimeArray[key].uptime + "%" + "</br>"


        + "</div>"; 
    }
}

Any help that you can offer would be greatly appreciated.

Thanks

You can use key to access to the dates you want I think. Because you are using the title as a key. So when you are accessing the array, you are writing uptimeArray["2016-07"].enabledfor example in the for loop.

I used this simplified code to access the data you wanted.

var uptimeArray = {

    "2015-12": {
        "enabled": 1261394905,
        "down": 0,
        "uptime": 100
    },
    "2016-01": {
        "enabled": 2678400000,
        "down": 294616,
        "uptime": 99.989
    },
    "2016-02": {
        "enabled": 2505600000,
        "down": 294572,
        "uptime": 99.988
    },
    "2016-03": {
        "enabled": 2678400000,
        "down": 545954,
        "uptime": 99.98
    },
    "2016-04": {
        "enabled": 2592000000,
        "down": 0,
        "uptime": 100
    },
    "2016-05": {
        "enabled": 2678400000,
        "down": 0,
        "uptime": 100
    },
    "2016-06": {
        "enabled": 2592000000,
        "down": 5358367,
        "uptime": 99.793
    },
    "2016-07": {
        "enabled": 1848549928,
        "down": 0,
        "uptime": 100
    },
    "total": {
        "enabled": 18834744833,
        "down": 6493509,
        "uptime": 99.966
    }
}

for (key in uptimeArray) {
        // THIS IS WHERE I WOULD LIKE TO ACCESS THE TITLE STRING OF EACH OBJECT
       console.log(key);
    }

and the output in the javascript console is thus:

2015-12
2016-01
2016-02
2016-03
2016-04
2016-05
2016-06
2016-07
total

Brilliant, Thanks Özgür