I am trying to render a multilevel array using the map function. How do I display a subarray?
The «Home» variable should return a render of a JSON array. The array itself is multilevel, each array has a subarray category_forums. The .map() function handles the first-level arrays without problems, but when I try to access the subarray, errors appear (indicating that another character is expected).
I tried to process differently (.map(function (category) {
instead .map(category =>) but nothing changes when I try to use the map function for category.category_forums.
let Home = {
render : async () => {
let categories = await getCategories()
// getCategories returns JSON:
// [
// {
// "category_id":"1", "category_title":"First category",
// "category_forums":[
// { "forum_id":"1", "forum_name":"Forum1" },
// { "forum_id":"2", "forum_name":"Forum2" }
// ]
// },
// {
// "category_id":"2", "category_title":"Second category",
// "category_forums":[
// { "forum_id":"3", "forum_name":"Forum3" },
// { "forum_id":"4", "forum_name":"Forum4"}
// ]
// }
// ]
let view = /*html*/`
<section class="section">
<h1> Categories </h1>
<ul>
${ categories.map(category =>
/*html*/`<li><a href="#/p/${category.category_id}">${category.category_title}</a></li>`
// here i need to process objects in category.category_forums
).join('\n ')
}
</ul>
</section>
`
return view
}
, after_render: async () => {
}
}
Currently, the function returns only category_id and category_title, and instead of category_forums it returns [object Object].