У меня есть приложение реагирования, которое показывает элементы из вложенного json с использованием функции карты, однако одна из категорий может быть пустой, что приводит к сбою всего приложения, я протестировал функцию foreach для того же json, используя js для каждой функции, и это сработало отлично, я пытаюсь перевести ту же логику, которая решит проблему с пустой категорией.
здесь мой для каждого теста на том же JSON, но со списками:
Test1
let obj = {
"name": "Menu",
"children": [
{
"type": "category",
"name": "Burgers",
"children": [
{
"type": "item",
"name": "Burger 1",
"children": [
{
"type": "modifier",
"name": "Promo",
"children": [
{
"type": "item",
"name": "Promo 1"
}
]
},
{
"type": "group",
"name": "Drinks",
"children": [
{
"type": "item",
"name": "Coke"
},
{
"type": "item",
"name": "Light Coke"
},
{
"type": "item",
"name": "Sprite"
},
{
"type": "item",
"name": "Fanta"
}
]
},
{
"type": "modifier",
"name": "Without",
"children": [
{
"type": "ingredient",
"name": "Onion"
},
{
"type": "ingredient",
"name": "Tomato"
},
{
"type": "ingredient",
"name": "Pickles"
}
]
}
]
},
{
"type": "item",
"name": "Burger 2",
"children": [
{
"type": "modifier",
"name": "Promo",
"children": [
{
"type": "item",
"name": "Promo 1"
}
]
},
{
"type": "group",
"name": "Drinks",
"children": [
{
"type": "item",
"name": "Coke"
},
{
"type": "item",
"name": "Light Coke"
},
{
"type": "item",
"name": "Sprite"
},
{
"type": "item",
"name": "Fanta"
}
]
},
{
"type": "modifier",
"name": "Without",
"children": [
{
"type": "ingredient",
"name": "Onion"
},
{
"type": "ingredient",
"name": "Tomato"
},
{
"type": "ingredient",
"name": "Pickles"
}
]
}
]
},
{
"type": "item",
"name": "Coming Soon Offers"
}
]
},
{
"type": "category",
"name": "Pizzas",
"children": [
{
"type": "item",
"name": "Pizza 1",
"children": [
{
"type": "modifier",
"name": "Promo",
"children": [
{
"type": "item",
"name": "Promo 1"
}
]
},
{
"type": "group",
"name": "Drinks",
"children": [
{
"type": "item",
"name": "Coke"
},
{
"type": "item",
"name": "Light Coke"
},
{
"type": "item",
"name": "Sprite"
},
{
"type": "item",
"name": "Fanta"
}
]
},
{
"type": "modifier",
"name": "Without",
"children": [
{
"type": "ingredient",
"name": "Onion"
},
{
"type": "ingredient",
"name": "Mashrooms"
},
{
"type": "ingredient",
"name": "Olives"
}
]
}
]
},
{
"type": "item",
"name": "Pizza 2",
"children": [
{
"type": "modifier",
"name": "Promo",
"children": [
{
"type": "item",
"name": "Promo 1"
}
]
},
{
"type": "group",
"name": "Drinks",
"children": [
{
"type": "item",
"name": "Coke"
},
{
"type": "item",
"name": "Light Coke"
},
{
"type": "item",
"name": "Sprite"
},
{
"type": "item",
"name": "Fanta"
}
]
},
{
"type": "modifier",
"name": "Without",
"children": [
{
"type": "ingredient",
"name": "Onion"
},
{
"type": "ingredient",
"name": "Mashrooms"
},
{
"type": "ingredient",
"name": "Olives"
}
]
}
]
}
]
}
]
}
let body = document.querySelector('body');
function print(obj){
let str = `<li>${obj.name}</li>`;
if(obj.children){
str += '<ul>'
for(let c of obj.children) str += print(c)
str += '</ul>'
}
return str;
}
document.body.innerHTML = print(obj);
Тест 2
const buildMenu = (data) => {
let ul = document.createElement('ul');
data.children.forEach(i => {
let li = document.createElement('li');
li.innerText = i.name;
li.className = i.type;
if (i.children) li.appendChild(buildMenu(i));
ul.appendChild(li);
});
return ul;
};
let data = {
"name": "Menu",
"children": [{
"type": "category",
"name": "Burgers",
"children": [{
"type": "item",
"name": "Burger 1",
"children": [{
"type": "modifier",
"name": "Promo",
"children": [{
"type": "item",
"name": "Promo 1"
}]
},
{
"type": "group",
"name": "Drinks",
"children": [{
"type": "item",
"name": "Coke"
},
{
"type": "item",
"name": "Light Coke"
},
{
"type": "item",
"name": "Sprite"
},
{
"type": "item",
"name": "Fanta"
}
]
},
{
"type": "modifier",
"name": "Without",
"children": [{
"type": "ingredient",
"name": "Onion"
},
{
"type": "ingredient",
"name": "Tomato"
},
{
"type": "ingredient",
"name": "Pickles"
}
]
}
]
},
{
"type": "item",
"name": "Burger 2",
"children": [{
"type": "modifier",
"name": "Promo",
"children": [{
"type": "item",
"name": "Promo 1"
}]
},
{
"type": "group",
"name": "Drinks",
"children": [{
"type": "item",
"name": "Coke"
},
{
"type": "item",
"name": "Light Coke"
},
{
"type": "item",
"name": "Sprite"
},
{
"type": "item",
"name": "Fanta"
}
]
},
{
"type": "modifier",
"name": "Without",
"children": [{
"type": "ingredient",
"name": "Onion"
},
{
"type": "ingredient",
"name": "Tomato"
},
{
"type": "ingredient",
"name": "Pickles"
}
]
}
]
},
{
"type": "item",
"name": "Coming Soon Offers"
}
]
},
{
"type": "category",
"name": "Pizzas",
"children": [{
"type": "item",
"name": "Pizza 1",
"children": [{
"type": "modifier",
"name": "Promo",
"children": [{
"type": "item",
"name": "Promo 1"
}]
},
{
"type": "group",
"name": "Drinks",
"children": [{
"type": "item",
"name": "Coke"
},
{
"type": "item",
"name": "Light Coke"
},
{
"type": "item",
"name": "Sprite"
},
{
"type": "item",
"name": "Fanta"
}
]
},
{
"type": "modifier",
"name": "Without",
"children": [{
"type": "ingredient",
"name": "Onion"
},
{
"type": "ingredient",
"name": "Mashrooms"
},
{
"type": "ingredient",
"name": "Olives"
}
]
}
]
},
{
"type": "item",
"name": "Pizza 2",
"children": [{
"type": "modifier",
"name": "Promo",
"children": [{
"type": "item",
"name": "Promo 1"
}]
},
{
"type": "group",
"name": "Drinks",
"children": [{
"type": "item",
"name": "Coke"
},
{
"type": "item",
"name": "Light Coke"
},
{
"type": "item",
"name": "Sprite"
},
{
"type": "item",
"name": "Fanta"
}
]
},
{
"type": "modifier",
"name": "Without",
"children": [{
"type": "ingredient",
"name": "Onion"
},
{
"type": "ingredient",
"name": "Mashrooms"
},
{
"type": "ingredient",
"name": "Olives"
}
]
}
]
}
]
}
]
};
document.body.appendChild(buildMenu(data));