0

I need to convert a JSON response to nested div elements and append them using jquery. For example, my JSON response is something like this.

[
    {
        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

<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. But I am getting undefined for subCategories. How do I achieve something like this using jquery?

2
  • 1
    use map instead of each and get after the map. Commented Mar 12, 2018 at 12:01
  • oh!!! I got it working using map and then join the array. Thanks gurvinder Commented Mar 12, 2018 at 12:20

2 Answers 2

1

I understood my mistake, Since forEach method doesn't return anything, map is the right one to use, to return an array. Then join the array, otherwise, there will be commas in your 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>`
    );
});
Sign up to request clarification or add additional context in comments.

Comments

0

One way to achieve this is to construct your html as a string and use html to update the 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>

4 Comments

As per the requirement the secondary ul is not inside li.
@SarathSRajendran As per the standard, The children (direct descendants) of a ul element must all be li elements.
@SarathSRajendran if you know that, im not sure what is the point of the comment. Are you trying to suggest that i should answer invalid HTML code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.