How to get key and value of an object when using *ngFor in Angular?
Hello! I'm new to Angular and TypeScript, and I have a problem working with objects in templates. I have an object with user data:
```typescript
interface Users {
[key: string]: { name: string; age: number };
}
const users: Users = {
user1: { name: 'Ivan', age: 30 },
user2: { name: 'Maria', age: 25 },
user3: { name: 'Petr', age: 35 }
};
```
I'm trying to display this data in a template using *ngFor, but I can't figure out how to get both the key (user1, user2) and the value (object with name and age). If I simply do:
```html
{{ user }}
```
Then Angular throws an error because *ngFor only works with arrays. I tried using Object.keys(), but I don't know how to properly apply it in the template. Please advise. No answers yet.
Log in to leave an answer
Blogs