how to loop inside object array Angular

Viewed 117

I want to display values from my json but I don't know how to do it. Is it possible to loop inside an object array ? i don't know if the keyvalue pipe can help me but I want to do without.

how to get the student2 and also the name to display it ?

thanks everyone.

json

{
 "student": {
   "student1": [],
   "student2": [
    {
      "id": "123",
      "name": "boot"
    },
    "student3": [],
   ]
  },
 "teacher": {
   "teacher1": [],
   "teacher2": [
    {
      "id": "123456",
      "name": "toto"
    },
   ]
  }
}

ts.file

get(){
 this.service.getAll().subscribe((data:any) => {
  object.keys(data).length > 0;
 })
}
1 Answers

Assuming your JSON object from your GET request looks like the one you posted above simply do:

get(){
 this.service.getAll().subscribe((data:any) => {
  data.student.forEach(element => {
    for (let key in element) {
        console.log("      key:", key, "value:", element[key]);
        for (let val in element[key]) {
            console.log("      value:", val);
        }
    }
  });
 })
}
Related