I'm currently busy with an Angular project where I receive an object containing an array of objects from an API.
"reportData" is the object being sent to the API as a parameter through my service.
Example of data sent back from the API:
I want to loop through the array and push the product names to an array and the same with the averageQuantityOrdered but I don't know how to loop through the array and get the values.
One thing that is confusing me is that when you look at the attached screenshot it looks like the result is an object containing an array of objects. And I think that is why I can't loop through it, since technically its an object and not an array.
I tried something like this (retrievedData is the array of objects) but then I get the error "Cannot read property 'forEach' of undefined":
retrievedData: any;
this.retrievedData.array.forEach(element => {
this.productNames.push(element.ProductName);
});
I use a service to retrieve the data:
onSubmit(form:NgForm)
{
this.service.postReportData().subscribe(
res => {
this.retrievedData = res;
console.log(this.retrievedData);
},
err => {
console.log(err);
}
);
}
Any help would be appreciated!