When I make a call to an API, it returns a response in the form of a map:
{thomas: 3, test70: 2, tim: 2, elin: 2, sumeet12: 1}
I tried iterating over this response in Angular, but encountered an error.
Error Encountered:
This expression is not callable. Type 'Number' has no call signatures.ts(2349)
this.testObject.forEach((value: number, key: string) => {
console.log(key, value);
});
Iterating over Typescript Map
However, I found that I was able to iterate over the response in HTML using the following code:
In HTML:
<div *ngFor="let item of testObject | keyvalue: originalOrder; let i=index">
<span *ngIf="i<3">
Key: <b>{{item.key}}</b> and Value: <b>{{item.value}}</b>
</span>
</div>
In the .ts file:
testObject: { [key: string]: number };
showresult: any;
getQuizResult(id){
this.service.getResult(id).subscribe((stndrdResp: Response) => {
this.showresult = stndrdResp.obj;
this.testObject = this.showresult;
});
}
My main goal is to iterate over the backend API response within the getQuizResult function so that I can perform certain actions based on the data. Any suggestions would be greatly appreciated.