I have an array of objects that list out authors and books. They're all in one array, but I could split them out into two arrays (authors and books) if that makes things more straightforward. The current single array is like this:
[{authorId: 1, author: 'Jane Doe', isAuthor: true, type: 'Autobiography'},
{authorId: 2, author: 'John Smith', isAuthor: true, type: 'Fiction'},
{authorId: 3, author: 'Bill Bradley', isAuthor: true, type: 'Non-Fiction'},
{authorId: 4, author: 'Jack Thomas', isAuthor: true, type: 'Autobiography'},
{authorId: 5, author: 'Molly McNeil', isAuthor: true, type: 'Autobiography'},
{authorId: 1, bookId: 50, bookTitle: 'Title 1', isAuthor: false, author: 'Jane Doe', type: 'Autobiography'},
{authorId: 1, bookId: 51, bookTitle: 'Title 2', isAuthor: false, author: 'Jane Doe', type: 'Autobiography'},
{authorId: 2, bookId: 52, bookTitle: 'Title 3', isAuthor: false, author: 'John Smith', type: 'Fiction'},
{authorId: 3, bookId: 53, bookTitle: 'Title 4', isAuthor: false, author: 'Bill Bradley', type: 'Non-Fiction'},
{authorId: 2, bookId: 54, bookTitle: 'Title 5', isAuthor: false, author: 'John Smith', type: 'Fiction'},
{authorId: 4, bookId: 55, bookTitle: 'Title 6', isAuthor: false, author: 'Jack Thomas', type: 'Autobiography'},
{authorId: 4, bookId: 56, bookTitle: 'Title 7', isAuthor: false, author: 'Jack Thomas', type: 'Autobiography'}]
The actual array of objects can be several thousand so I need to keep performance in mind.
I need to display everything in a specific way and I can't seem to get this to work. Each author should have its own LI with a UL within that lists each book. However, that entire block should be under a parent LI with the "type". Something like this:
<ul id="myList">
<li class="parent">
<span class="header">Autobiography</span>
<ul>
<li>
<span class="author">Jane Doe</span>
<ul>
<li><span class="bookTitle">Title 1</span></li>
<li><span class="bookTitle">Title 2</span></li>
</ul>
</li>
</ul>
</li>
<li class="parent">
<span class="header">Autobiography</span>
<ul>
<li>
<span class="author">Jack Thomas</span>
<ul>
<li><span class="bookTitle">Title 6</span></li>
<li><span class="bookTitle">Title 7</span></li>
</ul>
</li>
</ul>
</li>
<li class="parent">
<span class="header">Autobiography</span>
<ul>
<li>
<span class="author">Molly McNeil</span>
</li>
</ul>
</li>
<li class="parent">
<span class="header">Fiction</span>
<ul>
<li>
<span class="author">John Smith</span>
<ul>
<li><span class="bookTitle">Title 3</span></li>
<li><span class="bookTitle">Title 5</span></li>
</ul>
</li>
</ul>
</li>
<li class="parent">
<span class="header">Non-Fiction</span>
<ul>
<li>
<span class="author">Bill Bradley</span>
<ul>
<li><span class="bookTitle">Title 4</span></li>
</ul>
</li>
</ul>
</li>
</ul>
I think I should try to transfor my array of objects first before display them in the DOM, but I can't seem to get things quite right. Struggling to find a solution that keeps performance in mind. Any help is appreciated. Thanks!
EDIT: there's also a use case where an author may not have any books, but I'd still like them listed in the list. Array and HTML updated above