简体   繁体   English

通过 Vanilla JS / JQuery 根据元素嵌套处理对象数组

[英]Processing an array of objects according to element nesting via Vanilla JS / JQuery

I have an array of objects that I have to process and then include data from it to HTML.我有一组对象,我必须处理这些对象,然后将其中的数据包含到 HTML 中。 The problem is that now everything is displayed one by one and if I try to toggle class to li element, it toggles to all li elements.问题是现在一切都被一一显示,如果我尝试将 class 切换到 li 元素,它会切换到所有 li 元素。 The data can be changed dynamically so I cannot access elements by ID.数据可以动态更改,因此我无法通过 ID 访问元素。

I want to access the "main" category first (desserts, water, tea), then be able to proceed to subcategory of the selected main category etc. Further I will create a menu like in the screenshot (a one menu, a pic shows different states of it)我想首先访问“主要”类别(甜点、水、茶),然后能够继续到所选主要类别的子类别等。此外,我将创建一个类似屏幕截图的菜单(一个菜单,图片显示它的不同状态)

I have 2 problems now:我现在有两个问题:

  1. For some reason the main category isn't showing at all - why is it happening?由于某种原因,主要类别根本没有显示 - 为什么会发生?
  2. How can I access the elements according to hierarchy and nesting?如何根据层次结构和嵌套访问元素?

 const menu = [ { id: 1, name: "Desserts", groups: [ { id: 2, name: "Cold", groups: [ { id: 3, name: "Ice Cream", groups: [] }, { id: 4, name: "Cold brew coffee", groups: [] } ] }, { id: 5, name: "Hot", groups: [ { id: 6, name: "Pancakes", groups: [] }, { id: 7, name: "Apple pie", groups: [] } ] } ] }, { id: 8, name: "Water", groups: [] }, { id: 7, name: "Tea", groups: [ { id: 8, name: "Green tea", groups: [ { id: 9, name: "With Jasmine", groups: [] }, { id: 10, name: "Plain", groups: [] } ] }, { id: 11, name: "Black Tea", groups: [] } ] } ]; let menuEl = document.querySelector(".funding__categories"); addElements(menuEl, menu[0].groups); function addElements(parent, arr) { let allCategories = parent.appendChild(document.createElement("ul")); allCategories.classList.add("parent"); arr.forEach((el) => { let subCategory = allCategories.appendChild(document.createElement("li")); subCategory.dataset.id = el.id; subCategory.textContent = el.name; if (el.groups.length > 0) addElements(subCategory, el.groups); }); }
 <div class="funding__categories"></div>

The first problem is because you're passing menu[0].groups to the function.第一个问题是因为您将menu[0].groups传递给 function。 That skips over the top-level categories and starts at the items nested under Desserts .这会跳过顶级类别并从嵌套在Desserts下的项目开始。 Pass menu as the argument.传递menu作为参数。

I don't understand the second question.我不明白第二个问题。 Access them in what way?以什么方式访问它们?

 const menu = [ { id: 1, name: "Desserts", groups: [ { id: 2, name: "Cold", groups: [ { id: 3, name: "Ice Cream", groups: [] }, { id: 4, name: "Cold brew coffee", groups: [] } ] }, { id: 5, name: "Hot", groups: [ { id: 6, name: "Pancakes", groups: [] }, { id: 7, name: "Apple pie", groups: [] } ] } ] }, { id: 8, name: "Water", groups: [] }, { id: 7, name: "Tea", groups: [ { id: 8, name: "Green tea", groups: [ { id: 9, name: "With Jasmine", groups: [] }, { id: 10, name: "Plain", groups: [] } ] }, { id: 11, name: "Black Tea", groups: [] } ] } ]; let menuEl = document.querySelector(".funding__categories"); addElements(menuEl, menu); function addElements(parent, arr) { let allCategories = parent.appendChild(document.createElement("ul")); allCategories.classList.add("parent"); arr.forEach((el) => { let subCategory = allCategories.appendChild(document.createElement("li")); subCategory.dataset.id = el.id; subCategory.textContent = el.name; if (el.groups.length > 0) addElements(subCategory, el.groups); }); }
 <div class="funding__categories"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM