so i am building a website that uses object data that has been fetched from my database and i am trying to turn that data into valid html markup with a recursive function, this seems like something that would be possible commonly created so i am hoping i am just overlooking the complexity of this function,
this is what it looks like so far with the array loop in it, i still need to check for objects and strings
const objectData = {
main: {
class: "unordered list top",
ul: [
{
class: "list items top",
li: {
class: "unordered list bottom",
ul: [
{
class: "list items bottom",
li: {
h4: "bottom item 1",
},
},
{
class: "list items bottom",
li: {
h4: "bottom item 2",
},
},
{
class: "list items bottom",
li: {
h4: "bottom item 3",
},
},
],
},
},
],
},
};
function iterate({ main }) {
for (const prop in main) {
if (Array.isArray(main[prop])) {
return main[prop]
.map(() => {
`<${main}>${iterate(main[prop])}</${main}>`;
})
.join("");
}
}
}
document.addEventListener("DOMContentLoaded", () => {
// document.getElementById("body").innerHTML = iterate(objectData);
console.log(iterate(objectData));
});
i have only included the array loop but im hoping someone understands where im going with this function, or maybe i am just going down the rabbit hole and not looking for an easier way do do this