2017-05-30 9 views
2

JSONレスポンスからデータを選択しようとしていますが、必要な値をすべて取得できません。ここでJavaScript - 同じキー名の要素を印刷できません

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

だから、アイデアは以下の通り各被験者から部屋コードと名前を選択することです。

私自身のコードでこれを実行しようとしましたが、何らかの理由で部屋名の1つをスキップします。 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) 

を抜けてどこ出力は以下の通りですか?

+0

場で、そのまま、それは動作します:https://jsfiddle.net/p474djan/ –

+0

方法によって、JSON有効ではない、不要な "、"があり、jsfiddleのコードが動作する – ChaosPattern

答えて

1

あなたはArray.prototype.map()

を使用することができますマップ()メソッドは、配列の全ての要素に 与えられた関数を呼び出した結果、新しい配列を作成します。

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

console.log(res); 

SNIPPET

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

これはあなたが欲しかったのですか?

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

私はあなたのコードを実行し、すべてが大丈夫でした。あなたのコードを向上させることができたが:フィドルにあなたのコードを貼り付け

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);         
      }         
     } 
    } 
} 
1
yourobject.reservations.forEach(function(a){a.resources. 
    forEach(function(room){console.log({"code":room.code,"name":room.name})})}) 
関連する問題