Nicster15
Nicster15

Reputation: 169

Angular 4 | Looping Though Multi-Dimensional Array

I have a multi-dimensional array with two keys, it looks like so.

user: any = {};
// index is from a for loop to add to the user (this part works perfectly)
this.user[index++] = {
   Name: name,
   Age: age,
}

However my issue is I'd like to loop through that in my angular HTML template using *ngFor, however I receive a error saying it doesn't support it. So my question is, how can I loop through this user array and print out both name and age value of each key inside of the array?

My Attempt:

 <li *ngFor="let user of users">{{user.Name}} - {{user.Age}}</li>

The Error I Receive:

ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

Thank you.

Upvotes: 0

Views: 390

Answers (1)

Mohamed Abbas
Mohamed Abbas

Reputation: 2288

user: any = {}; is not an array!. try user: any = [];

let o = {}; this is an object. let arr = []; this is an array.

Upvotes: 1

Related Questions