Итак, я создаю «приложение», которое выбирает праздничные дни для текущего месяца. Выходные отображаются в таблице, которая создается в javascript путем циклического перебора по json и добавления его в строки. Дело в том, что l oop не может получить доступ к вложенным объектам, поэтому он просто отображает "[Object Object]"
. Это делается для страны (которая всегда вложена) и случайных праздников. Причина в том, что некоторые праздники отмечаются в разных частях страны, и поэтому, если существует много штатов / провинций, которые отмечают эту страну, она возвращает их в виде вложенного массива.
Можно ли это исправить или мне нужно все переписать?
Javascript:
// Filters holiday's to the current month
var currentMonthHolidays = data.response.holidays.filter(holiday => {
var holidayDate = new Date(holiday.date.iso);
var holidayMonth = holidayDate.getMonth();
var date = new Date();
var currentMonth = date.getMonth();
return currentMonth === holidayMonth;
})
// Build holiday table
function buildTable(data){
let table = document.getElementById('resultTable');
let col = [];
// Get the index of the api titles
for (let i = 0; i < currentMonthHolidays.length; i++) {
for (let key in currentMonthHolidays[i]) {
if (col.indexOf(key) === -1) {
col.push(key);
}
}
console.log(col)
}
while(table.firstChild){
table.removeChild(table.firstChild);
}
//Create table header row using the extracted headers above.
let tr = table.insertRow(-1); // table row.
for (let i = 0; i < col.length; i++) {
let th = document.createElement("th"); // table header.
th.innerHTML = col[i];
tr.appendChild(th);
}
// add json data to the table as rows.
for (let i = 0; i < currentMonthHolidays.length; i++) {
tr = table.insertRow(-1);
for (let j = 0; j < col.length; j++) {
let tabCell = tr.insertCell(-1);
tabCell.innerHTML = currentMonthHolidays[i][col[j]];
}
}
}
buildTable(currentMonthHolidays);
console.log(currentMonthHolidays);