0

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.

4
  • user: any = {}; is not an array!. try user: any = []; Commented Aug 13, 2017 at 5:29
  • 1
    ^ He is not trying to access object key -values, just the declaration is wrong. user: any[] = [] should fix it right? Commented Aug 13, 2017 at 5:36
  • @MohamedAbbas Agh, you're right. There goes an hour of my life trying to fix this issue. Thanks so much! Commented Aug 13, 2017 at 5:49
  • @Nicster15 Your're welcome :) , i convert my comment to answer. Check it. Commented Aug 13, 2017 at 6:15

1 Answer 1

1

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

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.