DEV Community

Jaime Rios
Jaime Rios

Posted on

Looping over object properties with ngFor in Angular

This post was published originally in medium.

Looping over object properties with ngFor in Angular
Hello again, this is post is about an interesting problem I found at work.

If you don’t know it yet. I’m talking about Angular 2+. FYI: Here is a post about why it is just Angular.

The problem

So you want to display the markup for a list, the values for this list come from the back end and for some reason instead of a good old array of objects you receive something like this.

{ 
  "car" : 
    { 
       "color" : "red",
       "model" : "2013"
    },
   "motorcycle": 
    { 
       "color" : "red",
       "model" : "2016"
    },
   "bicycle": 
    { 
       "color" : "red",
       "model" : "2011"
    }
}
Enter fullscreen mode Exit fullscreen mode

Then you try to use *ngFor but a wild error message appears:

Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
You might fix it in the back end so you get an array of objects, but ain’t no body got time for that. Don’t you worry child, I’ve got us.

The solution

In a perfect world, you would get an array of objects, since the world is not always, perfect. What you want to do, is to store all those objects within an array. Here is an over-simplified solution in plain old JavaScript.

Step 1. Get all the object keys. using Object.keys. This method returns an array of a given object’s own enumerable properties.

Step 2. Create an empty array. This is an where all the properties are going to live, since your new ngFor loop is going to point to this array, we gotta catch them all.

Step 3. Iterate throw all keys, and push each one into the array you created.

Here’s how that looks like in code.

// Evil response in a variable. Here are all my vehicles.
let evilResponse = { 
  "car" : 
    { 
       "color" : "red",
       "model" : "2013"
    },
   "motorcycle": 
    { 
       "color" : "red",
       "model" : "2016"
    },
   "bicycle": 
    { 
       "color" : "red",
       "model" : "2011"
    }
}
// Step 1. Get all the object keys.
let evilResponseProps = Object.keys(evilResponse);
// Step 2. Create an empty array.
let goodResponse = [];
// Step 3. Iterate throw all keys.
for (prop of evilResponseProps) { 
    goodResponse.push(evilResponseProps[prop]);
}
Enter fullscreen mode Exit fullscreen mode

Then you can assign the goodResponse to the class property you were trying to iterare trough in the first place.

That’s all folks.

Top comments (2)

Collapse
 
zerzerimohamed profile image
Mohamed Zerzeri

there's actually a little mistake in the end of the code :
goodResponse.push(evilResponseProps[prop]); must be modified to

goodResponse.push(evilResponse[prop]);

but this code helped me a lot, thanks a lot didn't know what to do without it ;)

Collapse
 
papaponmx profile image
Jaime Rios

Glad to help. Thanks for the feedback.