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.
var myArray = ['Value 1', ['Inner value 1', 'Inner value 2', 'Inner value 3', 'Inner value 4'], 'Value 2', 'Value 3', 'Value 4', 'Value 5', 'Value 6'];
Here is my JavaScript code:
function arrToUl(arr) {
var div = document.getElementById('myList');
var ul = document.createElement('ul');
for (var i = 0; i < arr.length; i++) {
if (arr[i] instanceof Array) {
var list = arrToUl(arr[i]);
} else {
var li = document.createElement('li');
li.appendChild(document.createTextNode(arr[i]));
console.log(ul.appendChild(li));
}
div.appendChild(ul);
}
}
arrToUl(myArray);
The above code is producing the following result:
<ul>
<li>Value 1</li>
<li>Inner Value 1</li>
<li>Inner Value 2</li>
<li>Inner Value 3</li>
<li>Inner Value 4</li>
<li>Value 2</li>
<li>Value 3</li>
<li>Value 4</li >
<li>Value 5</li >
<li>Value 6</li>
But the result should look like below:
<ul>
<li>Value 1
<ul>
<li>Inner Value 1</li>
<li>Inner Value 2</li>
<li>Inner Value 3</li>
<li>Inner Value 4</li>
</ul>
</li>
<li>Value 2</li>
<li>Value 3</li>
<li>Value 4</li>
<li>Value 5</li>
<li>Value 6</li>
Thank you for your help.
let list = document. getElementById( "myList" ); Step 3: Now iterate all the array items using JavaScript forEach and at each iteration, create a li element and put the innerText value the same as the current item, and append the li at the list.
let ul = document. createElement("ul"); for (i = 0; i <= favLanguages. length; i++); { let li = document. createElement("li"); // create li element.
In pure JavaScript, you can create a <li> element using the createElement() method, and then append it to the list with Node. appendChild() method.
Arrays are ordered collections of values. Sets are unordered collections of unique values. Dictionaries are unordered collections of key-value associations. Arrays, sets, and dictionaries in Swift are always clear about the types of values and keys that they can store.
Your function is called arrToUl
, so it should just do that: convert the array to an ul
.
Once you have the ul
you can insert it wherever you want, but take that outside the function.
Then it all becomes clear.
function arrToUl(arr) {
var ul = document.createElement('ul'), li;
for (var i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
li.appendChild(arrToUl(arr[i]));
} else {
li = document.createElement('li');
li.appendChild(document.createTextNode(arr[i]));
ul.appendChild(li);
}
}
return ul;
}
var myArray = ['Value 1', ['Inner value 1', 'Inner value 2', 'Inner value 3', 'Inner value 4'], 'Value 2', 'Value 3', 'Value 4', 'Value 5', 'Value 6'];
document.body.appendChild(arrToUl(myArray));
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.
var myArray = ['Value 1', ['Inner value 1', 'Inner value 2', 'Inner value 3', 'Inner value 4'], 'Value 2', 'Value 3', 'Value 4', 'Value 5', 'Value 6'];
function arrToUl(root, arr) {
var ul = document.createElement('ul');
var li;
root.appendChild(ul); // append the created ul to the root
arr.forEach(function(item) {
if (Array.isArray(item)) { // if it's an array
arrToUl(li, item); // call arrToUl with the li as the root
return;
}
li = document.createElement('li'); // create a new list item
li.appendChild(document.createTextNode(item)); // append the text to the li
ul.appendChild(li); // append the list item to the ul
});
}
var div = document.getElementById('myList');
arrToUl(div, myArray);
<div id="myList"></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With