access value of array by its index in Angualar ngFor directive

Published

I have a requirement to access the value of array item by index in ngFor directive angular

Let say i have entityList array.i also have columnNames array which gives me number of td’s to generate. so columnNames acts as a table header and entityList has data for it to display in tabular format.

entityList{
    "Id": 1,
    "Name": "Admin",
}
{
    "Id": 2,
    "Name": "Finance",
}

columnNames{
    "Id",
    "Name",
}


<table class="table">
  <thead>
    <tr>
      <th *ngFor="let column of columnNames" scope="col">
        {{column}}
      </th>
    </tr>
  </thead>

  <tbody>
    <tr *ngFor="let entity of entityList">
      <td *ngFor="let column of columnNames;let i = index">
        {{entity[i]}}
      </td>
    </tr>

  </tbody>
</table>

i know i can do it like below , however i want to print the value using index and not by name.

<tr *ngFor="let entity of entityList;let i = index">
    {{entity.Id}}
    {{entity.Name}}
</tr>

Source: Angular Questions

Published
Categorized as angular, ngfor Tagged ,

Answers

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq