How to display more than one object from an array


I have been trying to display some titles from a media gallery within a database. I can get the code to work for 1 object but not any others unless I retrieve them individually.

 $w('#accTtlTxt').text = accommItem.hotel[0].title + "Accommodation price (per person) - £" + accommItem.hotelPrice;

The above code works but what I’m trying to achieve are if there are 3 objects within the array, then it will display all of the .title from the 3 entries. The below code does this but if I have another trip that has say 2 or 4 items then the code returns ‘undefined’ Basically I’m looking for the code to display all of the ‘title’ entries.

$w('#accTtlTxt').text = accommItem.hotel[0].title + "\n" + accommItem.hotel[1].title + "\n" + accommItem.hotel[2].title + "Accommodation price (per person) - £" + accommItem.hotelPrice;

Anyone have any ideas? Thank you

let text = "";
  if(accommItem.hotel.length > 0){
  let itemsWithTitles = accommItem.hotel.filter(e => e.title);
  let titles = => e.title);
    text += titles.join("\n");
       text += "Accommodation price (per person) - £" + accommItem.hotelPrice;
    $w('#accTtlTxt').text = text;

@jonatandor35 This not only gave me the outcome but expanded my coding knowledge, thanks very much :slight_smile:

You’re welcome :slight_smile:

1 Like