I have a json that an object I try to transform into an array otherwise when I loop on it I get [object object], in my code I tried something that works but it only shows me the values of each field instead of showing me key => value.
How to display the key and values of my json in my html ? is there a better way to convert an object to an array ?
json
{
"toto": [
"titi",
"tata"
],
"foo": [
"foobar",
"footix"
]
}
ts.file
fetchPosts() {
let resp = this.summaryService.getAllSummery()
this.sub = resp.subscribe((res: Isummary[]) => {
this.summaryArray = res
});
}
interface
export interface Isummary {
toto:string[],
foo:string[]
}
html
<div*ngFor="let post of summaryArray | keyvalue">
<span>{{post.key}}</span>
<span *ngfor="let value of post.value">{{ value }}</span>
</div>
summaryArray's type isIsummary[]which an object type. So you are trying to loop on a Object Array, I guess it's normal if it display "object Object" in your html. If you want to display values which are inside, try with a second ngFor and use keyvalue pipe : angular.io/api/common/KeyValuePipe