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

时间:2017-05-30 10:41:46

标签: javascript arrays json key element

我正在尝试从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响应并在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)

为什么会发生这种情况的任何建议?

4 个答案:

答案 0 :(得分:2)

这是你想要的吗?

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>

答案 1 :(得分:1)

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

  

map()方法创建一个新数组,其中包含调用a的结果   为此数组中的每个元素提供了函数。

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);

答案 2 :(得分:1)

   yourobject.reservations.forEach(function(a){a.resources.
    forEach(function(room){console.log({"code":room.code,"name":room.name})})})

答案 3 :(得分:0)

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

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