简体   繁体   English

如何使用模板文字从 JSON 数据创建嵌套“列表”?

[英]How do I create a nested "list" from JSON data by using template literals?

I am trying to create a nested "list" using template literals.我正在尝试使用模板文字创建一个嵌套的“列表”。 I have put the word list in quotes because I am not creating an unordered or ordered list, this is just a list of items.我将单词列表放在引号中,因为我没有创建无序列表或有序列表,这只是一个项目列表。 This is the data I have.这是我掌握的数据。

let items = [{"id":1,"sub":[{"item":2},{"item":4},{"item":2},{"item":8}]},{"id":2,"sub":[{"item":3},{"item":5},{"item":4}]},{"id":3,"sub":[{"item":6},{"item":5},{"item":8}]}];

The result I expect is the following.我期望的结果如下。

<div class="tacos">
   <div>1
      <div>
        <div>2</div>
        <div>4</div>
        <div>2</div>
        <div>8</div>
      </div>
   </div>
   <div>2
        <div>3</div>
        <div>5</div>
        <div>8</div>
   </div>
   <div>3
        <div>6</div>
        <div>5</div>
        <div>8</div>
   </div>
</div>

From here I have the following code in js:从这里我在js中有以下代码:

function makeList(items){
   let wrap;
   for (const item of items) {

     wrap += `<div>${item}</div>`;

   }

   return wrap;

}

makeList(items);

From the code above this is what I get:从上面的代码中,我得到了:

<div>1</div>
<div>2</div>
<div>3</div>

From this step, I don't know how to get the sub-items.从这一步开始,我不知道如何获取子项。 I could make another template, but they are very similar, so I would like to reuse the current template.我可以制作另一个模板,但它们非常相似,所以我想重用当前的模板。 I have thought about self-referencing, but when I do that for some reason it doesn't work.我曾考虑过自我引用,但是当我出于某种原因这样做时,它不起作用。 Any help or guidance will be appreciated.任何帮助或指导将不胜感激。

Just add in an iteration over the .sub property, that returns each of those child <div> s joined by the empty string.只需在.sub属性上添加一个迭代,它返回由空字符串连接的每个子<div>

 let items = [{"id":1,"sub":[{"item":2},{"item":4},{"item":2},{"item":8}]},{"id":2,"sub":[{"item":3},{"item":5},{"item":4}]},{"id":3,"sub":[{"item":6},{"item":5},{"item":8}]}]; function makeList(items){ let wrap = ''; for (const item of items) { wrap += ` <div> ${item.id} ${item.sub.map(({ item }) => `<div>${item}</div>`).join('')} </div> `; } return wrap; } console.log(makeList(items));

暂无
暂无

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

相关问题 如何使用括号符号在对象文字上创建嵌套属性? - How do I create nested properties on object literals using bracket notation? 如何从列表视图模板传递数据以使用angular来编辑视图模板 - How do I pass data from the list view template to edit view template using angular 在javascript中,如何从json数据创建嵌套数组或对象? - In javascript, how do I create a nested array or object from json data? 如何为带有嵌套对象的JSON数据创建Mustache模板? - How to create a Mustache template for JSON data with nested objects? 如何使用标记的模板文字在子样式组件中获取父组件的 state? - How do I get the state of the parent component in a child styled component using tagged template literals? 如何遍历json对象的嵌套属性并创建新的数组列表? - How do I iterate through nested properties of an json object and create a new array list? 如何使用JSON数据创建CanvasJS图表? - How do I create a CanvasJS Chart using JSON data? 如何在 express js 中从 Json 数据中渲染列表,然后使用 react 和 map() 渲染它? - How do I render a list from Json data in express js, then render it using react and map()? 如何使用角度js将数据从json转换为对象列表 - How do i convert data from json to list of objects using angular js 如何获得:借助更大的对象常量数据集中的模板,对象常量数组? - How to get: Array of object literals with help of an template from a bigger data set of object literals?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM