I would like to create div's on the fly by iteration over the Objects in the JSON variable. I know how to create it if I had to iterate over Array of Arrays but not on Objects
My JSON is following
{
"physics" : {
"subjectCode": 101,
"subjectName": "Physics",
"sectionA": 200,
"sectionB": 500,
"sectionC": 158,
},
"chemistry" : {
"subjectCode": 102,
"subjectName": "Chemistry",
"sectionA": 200,
"sectionB": 500,
"sectionC": 158,
},
"maths" : {
"subjectCode": 102,
"subjectName": "Maths",
"sectionA": 200,
"sectionB": 500,
"sectionC": 158
}
}
In my Component I am storing the data in data variable
export class AboutComponent implements OnInit {
data: object;
public dataArray: any;
constructor(private _data: Data) { }
ngOnInit() {
this._data.getData()
.subscribe(data => {
console.log(data);
for (let key in data) {
console.log(data[key]);
this.dataArray = [];
this.dataArray.push(data[key]);
}
});
}
}
How to create 3 div - physics, chemistry and maths according to this JSON on the fly.
I am getting all the properties of the Objects but not the Objects itself.
The output is like this
{
"subjectCode": 102,
"subjectName": "Maths",
"sectionA": 200,
"sectionB": 500,
"sectionC": 158
}
instead of physics, chemistry, maths
Typescript provides for..of statements https://www.typescriptlang.org/docs/handbook/iterators-and-generators.html
I know some might flag it as a duplicate of
Angular 2: create divs dynamically
But kindly help me. I am a newbie and have wasted a lot of time in this.
Cannot find any answer with easy explanation.
Thanks in Advance.