I have a function I am using to take a generated nested array and turn it into nested ULs:
const prepareUL = (root, arr) => {
let ul = document.createElement('ul');
root.appendChild(ul);
arr.forEach(function(item) {
let li = document.createElement('li');
if (Array.isArray(item)) {
prepareUL(li, item);
ul.appendChild(li);
return
};
li.appendChild(document.createTextNode(item));
ul.appendChild(li);
});
}
The generated array looks like:
[
[
"text-ID1",
"Section Title 1",
"Section paragraph 1",
"Section Paragraph 2"
],
[
"text-ID2",
"Section Title 2",
"Section paragraph 1",
"Section Paragraph 2"
],
[
"text-ID3",
"Section Title 3",
"Section paragraph 1",
"Section Paragraph 2"
]
]
I need to get the second element from array and extract it and prepend it to the UL it belongs to as a div. So generated structure would look like:
<ul>
<div>Section Title 1</div>
<ul>
<li>Section paragraph 1</li>
<li>Section paragraph 2</li>
</ul>
<div>Section Title 3</div>
<ul>
<li>Section paragraph 1</li>
<li>Section paragraph 2</li>
</ul>
<div>Section Title 3</div>
<ul>
<li>Section paragraph 1</li>
<li>Section paragraph 2</li>
</ul>
</ul>
My untrained instinct is to use DOM manipulation after the fact, because I know how to do that, but it seems inelegant and like I should be able to do that here. Too new to understand it well enough.