JavaScript - Unable to print element with same key name

Viewed 78

I'm trying to pick data from a JSON-response and I can't get all the values I need.

Here is the JSON-response body:

{
  "status": "success",
  "reservations": [
    {
      "id": "38177",
      "subject": "subjectID",
      "modifiedDate": "2017-05-16T12:46:17",
      "startDate": "2017-05-30T08:00:00",
      "endDate": "2017-05-30T22:00:00",
      "resources": [
        {
          "id": "124",
          "type": "room",
          "code": "F407",
          "parent": {
            "id": "4",
            "type": "building",
            "code": "buildingF",
            "name": "buildingName"
          },
          "name": " F407 (atk 34)"
        }
      ],
      "description": ""
    },
    {
      "id": "38404",
      "subject": "subjectID",
      "modifiedDate": "2017-05-16T12:49:25",
      "startDate": "2017-05-30T08:00:00",
      "endDate": "2017-05-30T22:00:00",
      "resources": [
        {
          "id": "128",
          "type": "room",
          "code": "F411",
          "parent": {
            "id": "4",
            "type": "building",
            "code": "buildingF",
            "name": "buildingName"
          },
          "name": " F411 (atk 34)"
        }
      ],
      "description": ""
    },
    {
      "id": "38842",
      "subject": "subjectID",
      "modifiedDate": "2017-05-30T06:03:13",
      "startDate": "2017-05-30T08:00:00",
      "endDate": "2017-05-30T22:00:00",
      "resources": [
        {
          "id": "107",
          "type": "room",
          "code": "F211",
          "parent": {
            "id": "4",
            "type": "building",
            "code": "buildingF",
            "name": "buildingName"
          },
          "name": " F211 (room 50)"
        }
      ],
      "description": ""
    },
{
      "id": "40186",
      "subject": "subjectID",
      "modifiedDate": "2017-05-26T08:45:50",
      "startDate": "2017-05-30T09:00:00",
      "endDate": "2017-05-30T14:00:00",
      "resources": [
        {
          "id": "118",
          "type": "room",
          "code": "F312",
          "parent": {
            "id": "4",
            "type": "building",
            "code": "buildingF",
            "name": "buildingName"
          },
          "name": " F312 (room 48)"
        }
      ],
      "description": ""
    },
  ]
}

So the idea is to pick room code and name from each subject which are the following;

"code": "F407"
"name": "F407 (atk 34)"

"code": "F411"
"name": "F411 (atk 34)"

"code": "F211"
"name": "F211 (room 50)"

"code": "F312"
"name": "F312 (room 48)"

I've tried doing this with my own code, but it skips one of the room names for some reason. I go through the JSON-response with for loops and find the code and name inside resources and push them in an array;

var rooms = [];

for (var i = 0; i < json.reservations.length; i++) {
    if (json.reservations[i].resources != null) {
        for (var j = 0; j < json.reservations[i].resources.length; j++) 
            {
            var reservation = json.reservations[i];
            var resource = json.reservations[i].resources[j];

            if (resource.type === "room") {
                if (rooms.indexOf("code")) {                                        
                    rooms.push(resource.code + resource.name);
                }                                  
            }                                   
        }
    }
}

document.getElementById("pageOne").innerHTML = rooms.join("<br/>")

The output is the following where it leaves out the "name": "F411 (atk 34)"

F407 F407 (atk 34)
F411
F211 F211 (room 50)
F312 F312 (room 48)

Any suggestions why this is happening?

4 Answers
Related