Currently, I am engrossed in an Angular project where I am fetching an object containing an array of objects from an API.
The object being passed to the API as a parameter through my service is called "reportData".
Here is an example of the data retrieved from the API:
https://i.stack.imgur.com/PV0Nh.png
My goal is to iterate through the array, extract the product names and average quantity ordered, but I am unsure how to accomplish this task.
One aspect that confuses me is that upon examining the attached screenshot, it seems like the result is an object containing an array of objects. This might be why I am having trouble iterating through it, since technically it is an object and not an array.
I attempted something like this (where retrievedData is the array of objects), but encountered the error "Cannot read property 'forEach' of undefined":
retrievedData: any;
this.retrievedData.array.forEach(element => {
this.productNames.push(element.ProductName);
});
To retrieve the data, I utilize a service:
onSubmit(form:NgForm)
{
this.service.postReportData().subscribe(
res => {
this.retrievedData = res;
console.log(this.retrievedData);
},
err => {
console.log(err);
}
);
}
Any assistance on this matter would be greatly appreciated!