pdx-peculiarties/js/load.js
david 8d41998929
All checks were successful
PDX Pecularities Server / deploy (push) Successful in 2s
format js
2025-04-07 23:31:33 -07:00

38 lines
1.2 KiB
JavaScript

const loadSection = (sectionName) => {
fetch(`data/${sectionName}.json`)
.then(response => response.json())
.then(data => {
const container = document.getElementById(sectionName);
container.innerHTML = renderItems(data)
})
.catch(error => console.error('Error loading JSON:', error));
}
function cap(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
// Recursive function to handle strings, arrays, and objects
const renderItems = (items) => {
return items.map(item => {
if (typeof item === 'string') {
return `<p>${item}</p>`;
} else if (Array.isArray(item)) {
return renderItems(item); // recurse into nested array
} else if (typeof item === 'object') {
return `<p>${Object.entries(item).map(([key, value]) =>
// `${value}`).join('<br>')}</p>`;
`<strong>${cap(key)}:</strong> ${value}`).join('<br>')}</p>`;
} else {
return `<p>${String(item)}</p>`;
}
}).join('');
};
const sections = document.getElementsByClassName('section')
Array.from(sections).forEach(section => {
loadSection(section.id)
})