-1

I am using *ngFor to loop through the data and generate a dynamic table. But i only have access to the values of the object not the keys. How do you access the object keys using *ngFor WITHOUT USING PIPES

export class AppComponent {
  users: {}[] = [{
    firstName: 'John',
    lastName: 'Doe',
    email: 'johndoe@example.com'
  },
  {
    firstName: 'Jane',
    lastName: 'Doe',
    email: 'janedoe@example.com'
  },
  {
    firstName: 'Mary',
    lastName: 'Doe',
    email: 'marydoe@example.com'
  }
];
}
SONGSTER
  • 585
  • 3
  • 11
  • 28
  • 1
    you can use Object.keys in your app.component.ts file https://stackoverflow.com/questions/43446241/for-in-or-for-of-object-keys – Content Solutions Sep 16 '17 at 15:04
  • Possible duplicate of [Get array of object's keys](https://stackoverflow.com/questions/8763125/get-array-of-objects-keys) – monica Sep 16 '17 at 18:56

2 Answers2

0

You can use this to access the key of object

*ngfor="let user of users; i=index"
Ramy hakam
  • 522
  • 1
  • 6
  • 19
0

Since you do not want to use Pipe , You could simply get the keys using Object.keys; in TS

export class App {
  users = [{
    firstName: 'John',
    lastName: 'Doe',
    email: 'johndoe@example.com'
  },
  {
    firstName: 'Jane',
    lastName: 'Doe',
    email: 'janedoe@example.com'
  },
  {
    firstName: 'Mary',
    lastName: 'Doe',
    email: 'marydoe@example.com'
  }
];
  getKeys = Object.keys;

}

and in template

 <div *ngFor="let key of getKeys(users[0])">{{key + ' : ' + users[0][key]}}

DEMO

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396