How to iterate object keys using *ngFor


I want to iterate [object object] using *ngFor in Angular 2.

The problem is the object is not array of object but object of object which contains further objects.


  "data": {
    "id": 834,
    "first_name": "GS",
    "last_name": "Shahid",
    "phone": "03215110224",
    "role": null,
    "email": "[email protected]",
    "picture": **{ <-- I want to get thumb: url but not able to fetch that**
      "url": null,
      "thumb": {
        "url": null
    "address": "Nishtar Colony",
    "city_id": 2,
    "provider": "email",
    "uid": "[email protected]"

I know we can use pipe to iterate the object but how we can iterate further from object to object means data->picture->thum:url.

This question is tagged with angular ionic2

~ Asked on 2016-12-30 12:40:38

The Best Answer is


Angular 6.0.0

introduced a KeyValuePipe

See also

  selector: 'keyvalue-pipe',
  template: `<span>
    <div *ngFor="let item of object | keyvalue">
    <div *ngFor="let item of map | keyvalue">
export class KeyValuePipeComponent {
  object: {[key: number]: string} = {2: 'foo', 1: 'bar'};
  map = new Map([[2, 'foo'], [1, 'bar']]);


You can use a pipe

@Pipe({ name: 'keys',  pure: false })
export class KeysPipe implements PipeTransform {
    transform(value: any, args: any[] = null): any {
        return Object.keys(value)//.map(key => value[key]);
<div *ngFor="let key of objs | keys">

See also How to iterate object keys using *ngFor?

~ Answered on 2016-12-30 12:50:20


I think the most elegant way to do that is to use the javascript Object.keys like this (I had first implemented a pipe for that but for me, it just complicated my work unnecessary):

in the Component pass Object to template:

Object = Object;

then in the template:

<div *ngFor="let key of Object.keys(objs)">
   my key: {{key}}
   my object {{objs[key] | json}} <!-- hier I could use ngFor again with Object.keys(objs[key]) -->

If you have a lot of subobjects you should create a component that will print the object for you. By printing the values and keys as you want and on an subobject calling itselfe recursively.

Hier you can find an stackblitz demo for both methods.

~ Answered on 2017-08-25 11:36:17

Most Viewed Questions: