I am trying to create array of objects dynamically in Angular. I have this segment of code:
data:{title:any,date:any};
arr:any=[]
this.service.calendarBooking().subscribe((res: any) => {
for (let i = 0; i < res.data.length; i++) {
// Creating object from API Response
this.data={title:res.data[i].name,date:res.data[i].date}
// Pushing object to array
this.arr.push(this.data)
}
})
When I am printing the array, the array responses like this:
0: {title: "spss", date: "2021-01-30"}
1: {title: "spss", date: "2021-01-29"}
2: {title: "spss", date: "2021-01-28"}
But when I am trying to access the indexes(arr[0],arr[1]) the response is undefined, length of arr is zero
How to solve this issue"?
question from:
https://stackoverflow.com/questions/65882676/how-to-create-array-of-objects-dynamically-in-angular-11 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…