简体   繁体   English

如何使用jQuery从对象的嵌套数组创建嵌套的HTML

[英]How to create nested HTML from nested array of objects using jquery

I need to convert a JSON response to nested div elements and append them using jquery. 我需要将JSON响应转换为嵌套的div元素,并使用jquery附加它们。 For example, my JSON response is something like this. 例如,我的JSON响应就是这样。

[
    {
        name: "List one",
        arr: [
            {key: "A1"},
            {key: "B1"},
            {key: "C1"},
            {key: "D1"}
        ]
    },
    {
        name: "List two",
        arr: [
            {key: "A2"},
            {key: "B2"},
            {key: "C2"},
            {key: "D2"}
        ]
    },
    {...},
    {...}
]

and I need to build a div structure like this 我需要建立一个像这样的div结构

<ul>
    <li>List one</li>
    <ul>
        <li>A1</li>
        <li>B1</li>
        <li>C1</li>
        <li>D1</li>
    </ul>
    <li>List two</li>
    <ul>
        <li>A2</li>
        <li>B2</li>
        <li>C2</li>
        <li>D2</li>
    </ul>
    .......
</ul>

Currently this is how I tried, 目前,这就是我尝试过的方式

jsonObject.forEach((item)=>{
    var subCategories = item.arr.forEach((subCat)=>{
                            return `<li class="list-group-item">${subCat.key}</li>`
                        })

    $('#nested').append(
        `<li class="list-group-item">
            ${item.name}
            <ul>
                ${subCategories}
            </ul>


        </li>`
    );
});

This is somewhat similar to React method. 这有点类似于React方法。 But I am getting undefined for subCategories. 但是我对subCategories不确定。 How do I achieve something like this using jquery? 如何使用jquery实现类似的功能?

I understood my mistake, Since forEach method doesn't return anything, map is the right one to use, to return an array. 我理解了我的错误,因为forEach方法不返回任何内容,所以map是使用正确的方法来返回数组的方法。 Then join the array, otherwise, there will be commas in your HTML. 然后加入数组,否则,HTML中将包含逗号。

jsonObject.forEach((item)=>{
    var subCategories = item.arr.map((subCat)=>{
                            return `<li class="list-group-item">${subCat.key}</li>`
                        }).join('')

    $('#nested').append(
        `<li class="list-group-item">
            ${item.name}
            <ul>
                ${subCategories}
            </ul>
        </li>`
    );
});

One way to achieve this is to construct your html as a string and use html to update the div 实现此目的的一种方法是将html构造为字符串,然后使用html更新div

 $(function() { var jsonObject = [{name: "List one",arr: [{key: "A1"},{key: "B1"},{key: "C1"},{key: "D1"}]},{name: "List two",arr: [{key: "A2"},{key: "B2"},{key: "C2"},{key: "D2"}]}]; var html = "<ul>"; jsonObject.forEach((i) => { html += "<li>" + i.name + "</li>"; html += "<li>"; html += "<ul>"; i.arr.forEach((s) => { html += "<li>" + s.key + "</li>"; }); html += "</ul>"; html += "</li>"; }); html += "</ul>"; $("#nested").html(html); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="nested"></div> 

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

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