Add the following custom JavaScript function to your project's Story JavaScript area. It processes the $item Array you pass it to generate a Unorder List (<ul>) based String which you can use a <<print>> macro to display.
setup.listItems = function (list) {
var output = "";
if (Array.isArray(list)) {
output += '<ul class="inventory">';
list.forEach(function (item) {
if (typeof item === "string") {
output += '<li>' + item + '</li>';
}
else {
var keys = Object.keys(item);
output += '<li>';
if (keys.includes('name')) {
output += item.name;
}
if (keys.includesAny(['description', 'weight', 'contents'])) {
output += '<ul>';
if (keys.includes('description')) {
output += '<li>desc: ' + item.description + '</li>';
}
if (keys.includes('weight')) {
output += '<li>weight: ' + item.weight.toFixed(1) + '</li>';
}
if (keys.includes('contents')) {
output += '<li>contents:' + setup.listItems(item.contents) + '</li>';
}
output += '</ul>';
}
output += '</li>';
}
});
output += '</ul>';
}
return output;
}
note: The above uses recursion internally to handle the use-case where items can contain other items, which in turn can contain other items etc...
You use the above setup.listItems() function like so
Your inventory:\
<<= setup.listItems($items)>>
P.S. The above produces a HTML structure like the following...
Your inventory:\
<ul class="inventory">
<li>pizza</li>
<li>sandwich</li>
<li>lasagna</li>
<li>lunchbag
<ul>
<li>desc: holds several food items and lets you carry them all together</li>
<li>contents:
<ul class="inventory">
<li>pasta
<ul>
<li>desc: just like mom used to make</li>
<li>weight: 1.0</li>
</ul>
</li>
<li>orange juice
<ul>
<li>desc: sweet and fresh orange juice</li>
<li>weight: 0.5</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
... which you can style using CSS like the following within your project's Story Stylesheet area.
ul.inventory {
list-style: none;
margin-block-start: 0;
margin-left: 0;
}