How to fix *ngFor Error trying to diff '[object Object]'

Hello, I have problem to for looping json

ihave json like this

{
    "timetable": {
        "1": [
            {
                "id": 49,
                "subject": "Matematika",
                "teacher": "Andi Hidayat, S.Pd"
            },
            {
                "id": 47,
                "subject": "FISIKA",
                "teacher": "Andi Hidayat, S.Pd"
            }
        ],
        "2": [
            {
                "id": 26,
                "subject": "FISIKA",
                "teacher": "Andi Hidayat, S.Pd"
            }
        ],
        "3": [
            {
                "id": 50,
                "subject": "Biologi",
                "teacher": "Risma Fitriani, S.Pd"
            }
        ],
        "4": [],
        "5": [],
        "6": [],
        "7": []
    }
}

This is my HTML File

<ion-item  *ngFor="let data of item" >
		    	<div class="title">
		    		<ion-row>
			      		<ion-col col-12>
			      			<h3><b>{{item.subject}}</b></h3>
			      			<p>Teacher</p>
			      		</ion-col>
		    		</ion-row>
		      	</div>
		    </ion-item>

I have tryto fetch json but Error trying to diff '[object Object how to Fix that ?
thanks

*ngFor doesn’t work with objects, only arrays. That is what the error messge is telling your. Transform your data into an array.

What is item in your code exactly?

1 Like

item from this. @Sujan12

  get(value){
    return this.Json.getstudentJadwal(value).subscribe(data => this.item = data.timetable)
  }

so what i do ? change the API ?

For example. Or transform the data after you habe it.

But: If you change timetable from object to array your code still will not work, as each entry of the array would be another array, with 1 or 2 entries each. You have an additional level in your data that you have to take care of in your template.

2 Likes

any aternative solution sir @Sujan12 ?

item and data are horrible property or variable names. If you chose more descriptive ones, not only would your code be much more understandable, but it would be clearer to others as to how to help you.

Why are you looking for alternative solutions? I told you two: Change the data, or rework the data after you get it. There is no other way.