I originally tried using the globals namespace inside my loop, but that didn’t work as expected and had me stuck for a while. The fix was to pull the fields into the ^as_asset: call and then normalise them with .map(), giving each property a clean name (e.g. title, dateStartDisplay, dateEndDisplay) etc
Inside the forEach I just destructure the fields I need, check if the end date is different from the start date, and build the markup. This keeps it clean, avoids the confusing globals behaviour, and makes it easy to extend later with description, location, etc.
// Normalise raw data into cleaner objects
const items = rawItems.map(item => ({
title: item.asset_name,
dateStartDisplay: item['asset_attribute_start_date^date_format:j F Y'],
dateEndDisplay: item['asset_attribute_end_date^date_format:j F Y']
}));
items.forEach(item => {
const { title,
dateStartDisplay,
dateEndDisplay} = item;
const showEndDate = dateEndDisplay && dateEndDisplay !== dateStartDisplay;
listMarkup += `
<li>
<strong>${title}</strong><br>
${dateStartDisplay}${showEndDate ? ` to ${dateEndDisplay}` : ``}
</li>
`;...