Skip to content
Advertisement

JavaScript: How to create unordered list from array?

I have the following array and I want to create an unordered list from it, but I am having trouble generating the unordered list in the proper format. I have searched similar questions but none of the existing solutions work for my problem.

JavaScript

Here is my JavaScript code:

JavaScript

The above code is producing the following result:

JavaScript

But the result should look like below:

JavaScript

Thank you for your help.

Advertisement

Answer

You’ve appended all the <ul> elements to the myList <div>. To change that, I’ve added a new parameter to the arrToUl(root, arr) function.

The new parameter, root, determines who the created <ul> should be appended to, so if the function encounters a sub-array, it uses the previous list item as the root for the creation of the sub-list.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement