I am trying to dynamically create an HTML table based on a JSON file coming from a database:
var details = {
"BookingsByHour" : [
{
"Date": "2014-06-5T00: 00: 00.000Z",
"Breakdown": [
{
"Hour": "00",
"BookingCount": 560,
"LastYearDifference": 42
},{
"Hour": "01",
"BookingCount": 524,
"LastYearDifference": 34
},{
"Hour": "02",
"BookingCount": 448,
"LastYearDifference": 92
} // etc...
],
"Total": 5340
},{
"Date": "2014-06-60T00: 00: 00.000Z",
"Breakdown": [
{
"Hour": "00",
"BookingCount": 506,
"LastYearDifference": 46
},{
"Hour": "01",
"BookingCount": 432,
"LastYearDifference": 92
},{
"Hour": "02",
"BookingCount": 498,
"LastYearDifference": 37
}
],
"Total": 14339
},{
"Date": "2014-07-60T00: 00: 00.000Z",
"Breakdown": [
{
"Hour": "00",
"BookingCount": 506,
"LastYearDifference": 46
},{
"Hour": "01",
"BookingCount": 432,
"LastYearDifference": 92
},{
"Hour": "02",
"BookingCount": 498,
"LastYearDifference": 37
}
],
"Total": 14339
},
],
};
The first row needs be composed by the 'Hour' (more clear once you see the json data), from 00 to 23, 24 hours basically
From the second row it will be the same for each date, there will be a 'BookingCount' corresponding to each 'Hour'.
This will be replicated based on how many 'Date' there are in the JSON data
So far I got this but I am stuck, I can't find a way to add the 'BookingCount corresponding to each 'Hour' for every 'Date'.
$(document).ready(function(){
var hours = details.BookingsByHour[0].Breakdown.length-1;
wrapper = $('.hours');
for(i =0; i <= hours; i++ ){
wrapper.append("<td>"+i+ "</td>");
};
for(i =0; i<=details.BookingsByHour.length-1; i++){
$('.div').append("<tr><td>" + details.BookingsByHour[i].Date +"</td></tr>");
console.log(details.BookingsByHour[i].Breakdown[i].BookingCount);
}
});