Search code examples
arraysangularobjectngfor

Angular Looping through an array of objects


I have an array which appears as follows:

[
 {
  myTitle:"myTitle",
  myDate:"myDate",
  images: [
   { imageDate:"date", imagePath:"path"}
  ]
 }
]

In my HTML, i loop over the array:

<li *ngFor="let item of items">
   <div> {{ item.myTitle }}
</li>

What I'd like to do is pull a value for the images also. Something like this:

<li *ngFor="let item of items">
   <div> {{ item.myTitle }}
   <ul>
     <li *ngFor="let image of item">
       {{ image.imagePath }}
     </li>
   </ul>
</li>

Is it possible to loop through the images array and reference a specific value?


Solution

  • Try like this:

    <li *ngFor="let item of items">
       <div> {{ item.myTitle }} </div>
       <ul>
         <li *ngFor="let image of item.images">
           {{ image.imagePath }}
         </li>
       </ul>
    </li>