This commit is contained in:
parent
f39b8439c0
commit
8d41998929
42
js/load.js
42
js/load.js
@ -1,38 +1,38 @@
|
|||||||
|
|
||||||
const loadSection = (sectionName) =>{
|
const loadSection = (sectionName) => {
|
||||||
fetch(`data/${sectionName}.json`)
|
fetch(`data/${sectionName}.json`)
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
const container = document.getElementById(sectionName);
|
const container = document.getElementById(sectionName);
|
||||||
container.innerHTML = renderItems(data)
|
container.innerHTML = renderItems(data)
|
||||||
})
|
})
|
||||||
.catch(error => console.error('Error loading JSON:', error));
|
.catch(error => console.error('Error loading JSON:', error));
|
||||||
}
|
}
|
||||||
|
|
||||||
function cap(str) {
|
function cap(str) {
|
||||||
return str.charAt(0).toUpperCase() + str.slice(1);
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recursive function to handle strings, arrays, and objects
|
// Recursive function to handle strings, arrays, and objects
|
||||||
const renderItems = (items) => {
|
const renderItems = (items) => {
|
||||||
return items.map(item => {
|
return items.map(item => {
|
||||||
if (typeof item === 'string') {
|
if (typeof item === 'string') {
|
||||||
return `<p>${item}</p>`;
|
return `<p>${item}</p>`;
|
||||||
} else if (Array.isArray(item)) {
|
} else if (Array.isArray(item)) {
|
||||||
return renderItems(item); // recurse into nested array
|
return renderItems(item); // recurse into nested array
|
||||||
} else if (typeof item === 'object') {
|
} else if (typeof item === 'object') {
|
||||||
return `<p>${Object.entries(item).map(([key, value]) =>
|
return `<p>${Object.entries(item).map(([key, value]) =>
|
||||||
// `${value}`).join('<br>')}</p>`;
|
// `${value}`).join('<br>')}</p>`;
|
||||||
`<strong>${cap(key)}:</strong> ${value}`).join('<br>')}</p>`;
|
`<strong>${cap(key)}:</strong> ${value}`).join('<br>')}</p>`;
|
||||||
} else {
|
} else {
|
||||||
return `<p>${String(item)}</p>`;
|
return `<p>${String(item)}</p>`;
|
||||||
}
|
}
|
||||||
}).join('');
|
}).join('');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const sections = document.getElementsByClassName('section')
|
const sections = document.getElementsByClassName('section')
|
||||||
Array.from(sections).forEach(section =>{
|
Array.from(sections).forEach(section => {
|
||||||
loadSection(section.id)
|
loadSection(section.id)
|
||||||
})
|
})
|
Loading…
Reference in New Issue
Block a user