2

I've a nested array in this form

tests: [
  [{name:"Bob",score:40,subject:"Math"}, {name:"John",score:55,subject:"Math"}],
  [{name:"Alice",score:70,subject:"English"},{name:"John",score:68,subject:"English"}]
],
// ...

and I want to loop through and print while grouping the same subjects together as in:

<div *ngFor = "let test of tests;let i = index">
  <ul *ngFor = "let student of test[i] ;let n = index">
     <li>{{student.name+' '+student.score}}</li>
  </ul>
</div>

but it end up with the error:

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

the starts from the second loop. what am I missing here, please?

2
  • Please provide the valid JSON Commented Dec 22, 2018 at 12:08
  • Use Object.keys(<Item>) Commented Dec 22, 2018 at 12:18

4 Answers 4

2

just changed test[i] to test that is an array and you can iterate it, but test[i] is and object and can't be iterate.

ts code:

 tests = [
          [{name:"Bob",score:40,subject:"Math"},{name:"John",score:55,subject:"Math"}],
          [{name:"Alice",score:70,subject:"English"},{name:"John",score:68,subject:"English"}]
        ]

html:

<div *ngFor = "let test of tests;let i = index">
    <ul *ngFor = "let student of test ;let n = index">
        <li>{{student.name+' '+student.score}}</li>
    </ul>
</div>

DEMO

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

2 Comments

Thanks @Fateme that was quite handy. MashaAllah
please explain what changes you did , also by ul got replaced with div
1

If that's your array, you loop it like this:

<div *ngFor="let test of tests">
  <ul *ngFor="let student of test">
     <li>{{student.name+' '+student.score}}</li>
  </ul>
</div>

Comments

0

i'm not angular developer but try for loop :

change let student of test[i] to let student of tests[i] or let student of test

Comments

0

Try This

 let tests = [
              [{name:"Bob",score:40,subject:"Math"},{name:"John",score:55,subject:"Math"}],
              [{name:"Alice",score:70,subject:"English"},{name:"John",score:68,subject:"English"}]
            ];

<div *ngFor="let test of tests">
    <ul *ngFor="let student of test">
       <li>{{student.name+' '+student.score}}</li>
    </ul>
  </div>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.