JavaScript - 无法打印具有相同键名的元素

JavaScript - Unable to print element with same key name

我正在尝试从 JSON 响应中选取数据,但无法获得所需的所有值。

这里是JSON-响应正文:

{
  "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": ""
    },
  ]
}

所以我的想法是从以下每个主题中选择房间代码和名称;

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

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

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

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

我试过用我自己的代码这样做,但出于某种原因它跳过了其中一个房间名称。我使用 for 循环遍历 JSON-response 并在 resources 中找到代码和名称并将它们推送到数组中;

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/>")

输出如下,其中省略了 "name": "F411 (atk 34)"

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

对于为什么会发生这种情况有什么建议吗?

这是你想要的吗?

var json = {
  "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": ""
    },
  ]
};


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++){      rooms +=json.reservations[i].resources[j].code +" " + json.reservations[i].resources[j].name+"</br>";
     }  
    }
}
document.getElementById("pageOne").innerHTML = rooms;
<div id="pageOne"></div>

您可以使用Array.prototype.map()

The map() method creates a new array with the results of calling a provided function on every element in this array.

var res = data.reservations.map(function(_data) {
  return {
    code: _data.resources[0].id,
    name: _data.resources[0].name
  }
});

console.log(res);

片段

var data = {
  "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": ""
    },
  ]
};


var res = data.reservations.map(function(_data) {
  return {
    code: _data.resources[0].id,
    name: _data.resources[0].name
  }
});

console.log(res);

我执行了你的代码,一切正常。虽然您的代码可以改进:

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

            if (resource.type === "room") {                                     
                 rooms.push(resource.code + resource.name);                                
            }                                   
        }
    }
}
   yourobject.reservations.forEach(function(a){a.resources.
    forEach(function(room){console.log({"code":room.code,"name":room.name})})})