I am making this menu page, using wordpress as back end. So I fetched the elements, put them to template and appended them to a parent, but now I need to figure out how to sort menu items alphabetically. I don't know if I should write new function after fetching and appending or make sorting a part of the function series that fetches and appends data.
This is what I used:
const template = document.querySelector("#menutemplate").content;
const newSection = document.createElement("section");
const parent = document.querySelector("main");
function getMenu() {
fetch("MY LINK GOES HERE")
.then(res => res.json())
.then(showMenu)
}
function showMenu(menuItems) {
//console.log(menuItems)
menuItems.forEach(showItem)
}
function showItem(item) {
//console.log(item)
const clone = template.cloneNode(true);
clone.querySelector(".product").textContent=item.title.rendered
clone.querySelector(".price").textContent=item.acf.volunteer_price + " dkk";
newSection.appendChild(clone);
parent.appendChild(newSection);
}
getMenu();
menuItems.sort()?